我正在清理csv文件中的非分隔符逗号。我有3个python片段,它们以交互方式工作以更改分隔符,替换逗号并重新实例化逗号分隔符。重新测试后,我发现这个单一的脚本不起作用,我想对在单个脚本中运行这些脚本的最佳实践方法有一些反馈。
#!/usr/bin/env python
# changes delimiter
import csv
reader = csv.reader(open("commas.csv", "rU"), delimiter=',')
writer = csv.writer(open("commas2.csv", 'w'), delimiter=';')
writer.writerows(reader)
quit()
# replaces commas with spaces
s = open("commas2.csv").read()
s = s.replace(',', ' ')
f = open("commas3.csv", 'w')
f.write(s)
f.close()
quit()
# changes delimiter back to commas
import csv
reader = csv.reader(open("commas3.csv", "rU"), delimiter=';')
writer = csv.writer(open("commas4.csv", 'w'), delimiter=',')
writer.writerows(reader)
quit()
答案 0 :(得分:1)
正如你所说,你需要使用with
语句来正确关闭所有文件(好吧,你不需要,但这是最佳做法):
import csv
with open("commas.csv", "r") as fr:
with open("commas2.csv", "w") as fw:
reader = csv.reader(fr, delimiter=',')
writer = csv.writer(fw, delimiter=';')
writer.writerows(reader)
with open("commas2.csv", "r") as fr:
s = fr.read()
with open("commas3.csv", "w") as fw:
fw.write(s.replace(",", " "))
# changes delimiter back to commas
with open("commas3.csv", "r") as fr:
with open("commas4.csv", "w") as fw:
reader = csv.reader(fr, delimiter=';')
writer = csv.writer(fw, delimiter=',')
writer.writerows(reader)
但你可以通过文件一次传递来做到这一点,因为你已经在内存中拥有了所有的csv内容,你可以逐个单元地修改它们,而不是用不同的分隔符保存它们然后再更改分隔符:
import csv
with open("commas.csv", "r") as f:
reader = csv.reader(f)
new_data = [] # new csv 2d array
for line in reader:
l = list()
for cell in line: # go over every cell
l.append(cell.replace(",", " ")) # replace commas then save it to new array
new_data.append(l)
with open("commas2.csv", "w") as f:
writer = csv.writer(f)
writer.writerows(new_data)