gpt4 book ai didi

python - 使用 Python 写入 csv 文件中的特定列

转载 作者:太空狗 更新时间:2023-10-30 00:20:03 28 4
gpt4 key购买 nike

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

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

最佳答案

我是这样解决问题的

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:

关于python - 使用 Python 写入 csv 文件中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16695740/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com