嗨,我一直在試驗Winston Ewert的代碼示例從這個線程。Python:如何關閉我的CSV輸入和輸出文件?
Python: Removing duplicate CSV entries
但我不能收我的輸入和輸出文件。我究竟做錯了什麼?
write_outfile.close()
write_infile.close()
Traceback (most recent call last): File "Duplicates_01.py", line 26, in write_outfile.close() AttributeError: '_csv.writer' object has no attribute 'close'
import csv
write_infile = csv.reader(open('File1.csv', 'r'))
write_outfile = csv.writer(open('File2.csv', 'w'))
#write_infile = open('File1.csv', 'r')
#f1 = csv.reader(write_infile)
#f1 = csv.reader(write_infile, delimiter=' ')
#write_outfile = open('File2.csv', 'w')
#f2 = csv.writer(write_outfile)
#f2 = csv.writer(write_outfile, delimiter=' ')
phone_numbers = set()
for row in write_infile:
if row[1] not in phone_numbers:
write_outfile.writerow(row)
# f2.writerow(row)
phone_numbers.add(row[1])
# write_outfile.close()
# write_infile.close()
File1.csv
user, phone, email
joe, 123, [email protected]
mary, 456, [email protected]
ed, 123, [email protected]
你爲什麼不能關閉它們?你遇到了什麼錯誤?你爲什麼評論結束語? –