日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 csv读写_在python3中读取、更新和写入更新的CSV文件

發布時間:2023/12/4 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 csv读写_在python3中读取、更新和写入更新的CSV文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在創建一個應用程序來本質上充當一個資產數據庫,我試圖打開一個CSV文件,讀取值并相應地更新它們。我看到很多關于如何讀寫CSV文件的教程,但是我找不到一個詳細介紹如何迭代CSV文件和更新單個值的教程。理想情況下,與列表相比,使用字典似乎更容易,因此我可以按關鍵字(部件名稱)進行搜索。它在大多數情況下都能工作,但是我在將更新后的列表寫入CSV文件時遇到了問題。我得到了以下錯誤:“第155行,書面形式

返回自撰稿人(self.\u dict_to_列表(rowdict))

值錯誤:對關閉的文件執行I/O操作def write(part_name, part_num="null"):

with open("Database.csv", "r") as file_read:

fieldnames=["Part_Name", "Part_Num"]

csv_reader = csv.DictReader(file_read, fieldnames=fieldnames)

temp_list = [] # Create temp copy of the csv file

for line in csv_reader: # Reading the CSV file and storing it in temp_list

temp_list.append(line)

for line in temp_list: # Printing the original list to verify contents were read correctly

print (line)

for line in temp_list: # Reading each element in the list and updating the value if different from the value passed into the function

if (line["Part_Name"] == part_name and line["Part_Num"] != part_num):

line["Part_Num"] = part_num

for line in temp_list: # Printing out the new vesion of the temp_list to verify updates took

print (line)

with open("Database.csv", "w") as file_write:

csv_writer = csv.DictWriter(file_write, fieldnames=fieldnames)

for line in temp_list: # Attempting to write the updated temp_list to the csv file

csv_writer.writerow(line)

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python3 csv读写_在python3中读取、更新和写入更新的CSV文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。