1
如何在通過csv閱讀器運行之前編輯一個打開的csv文件的內容?編輯一個打開的csv文件
csvfile = open(input, mode='r', newline='', encoding='utf-8') # opening csv file
for line in csvfile:
line = line.replace(",", ";") #replacing comma with semicolon
print(line) # test if replace was successful (working)
csvfile.seek(0) # return to the beginning of the file
csv_read = csv.reader(csvfile, delimiter=";", quotechar='"') # use reader
這不工作,因爲我希望和csv.reader仍然讀取舊的未經編輯的文件。
我該如何解決這個問題?
爲什麼不使用原始分隔符?請注意,'line ='只是在循環中重新分配該名稱,它不會**更改底層文件。 – jonrsharpe
[使用Python進行內聯CSV文件編輯]的可能重複(http://stackoverflow.com/questions/16020858/inline-csv-file-editing-with-python) – wwii