我在文件中有数据,我需要将其写入特定列中的CSV文件。文件中的数据如下:
002100
002077
002147
我的代码是:
import csv
f = open ("file.txt","r")
with open("watout.csv", "w") as output:
for line in f :
c.writerows(line)
总是写在第一列。我怎么能解决这个问题? 感谢。
答案 0 :(得分:8)
这就是我解决问题的方法
f1 = open ("inFile","r") # open input file for reading
with open('out.csv', 'wb') as f: # output csv file
writer = csv.writer(f)
with open('in.csv','r') as csvfile: # input csv file
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
row[7] = f1.readline() # edit the 8th column
writer.writerow(row)
f1.close()
答案 1 :(得分:-10)