【发布时间】:2018-01-28 20:35:07
【问题描述】:
您好,我一直在尝试使用此线程中的 Winston Ewert 的代码示例。
Python: Removing duplicate CSV entries
但我无法关闭我的输入和输出文件。我做错了什么?
write_outfile.close()
write_infile.close()
回溯(最近一次调用最后):文件“Duplicates_01.py”,第 26 行,在 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()
文件1.csv
user, phone, email
joe, 123, joe@x.com
mary, 456, mary@x.com
ed, 123, ed@x.com
【问题讨论】:
-
为什么不能关闭它们?你遇到了什么错误?为什么要注释掉关闭语句?
标签: python csv python-3.5 linux-mint