8

我在文件中有数据,我需要将其写入特定列中的 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)

它总是写在第一列。我该如何解决这个问题?谢谢。

4

1 回答 1

14

这就是我解决问题的方法

f1 = open ("inFile","r") # open input file for reading

with open('out.csv', 'w',newline="") as f:up # 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()   

python 2用户替换

with open('out.csv', 'w',newline="") as f:

经过

with open('out.csv', 'wb') as f:
于 2013-05-26T11:01:00.957 回答