小编pig*_*ggy的帖子

Python:如何关闭我的CSV输入和输出文件?

嗨,我一直在试验Winston Ewert的代码示例.

Python:删除重复的CSV条目

但我无法关闭输入和输出文件.我究竟做错了什么?

write_outfile.close()

write_infile.close()

回溯(最近一次调用最后一次):文件"Duplicates_01.py",第26行,在write_outfile.close()中AttributeError:'_ csv.writer'对象没有属性'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()
Run Code Online (Sandbox Code Playgroud)

File1.csv

user, phone, email
joe, 123, joe@x.com
mary, 456, mary@x.com
ed, 123, ed@x.com
Run Code Online (Sandbox Code Playgroud)

python csv linux-mint python-3.5

1
推荐指数
1
解决办法
4181
查看次数

标签 统计

csv ×1

linux-mint ×1

python ×1

python-3.5 ×1