如何使用python读取csv文件的每一行并将其写入其他csv文件中?
答案 0 :(得分:0)
这是csv真的重要吗?听起来就像您可以一行一行地写
with open('csvtowriteto', 'wb') as writer:
with open('csvtoreadfrom', 'rb') as reader:
for line in reader:
writer.write(line)
答案 1 :(得分:-2)
一个简单的解决方案:在写入其他csv文件之前,请先读取并拆分每一行。
def filter(source, destination):
fs = open(source, 'r')
fd = open(destination, 'w')
txt = fs.readline()
while txt:
txt1 = txt.split(',')
txt2 = txt1[0]+','+txt1[1]
fd.write(txt2+'\n')
txt = fs.readline()
fs.close()
fd.close()
如果名称 =='主要':
过滤器(r'C:\ source.csv',r'C:\ destination.csv')