gpt4 book ai didi

python - 如何向包含特定值的行添加新列?

转载 作者:行者123 更新时间:2023-12-01 01:49:28 27 4
gpt4 key购买 nike

我是 Python 编码新手。我需要向包含特定单词的行添加新列。我有如下 csv 文件:

one  two three four five 
two one five three four
five two four three five

我需要将“2”添加到第二列中包含“two”的所有行。

像这样:

one  two three four five 2
two one five three four
five two four three five 2

提前致谢

最佳答案

你可以做类似的事情

import csv

with open('input.csv','r') as csvinput:
with open('output.csv', 'w') as csvoutput:
writer = csv.writer(csvoutput, lineterminator='\n')
reader = csv.reader(csvinput)

all = []

for row in reader:
if(row[1] == "two"):
row.append(2)
all.append(row)

writer.writerows(all)

关于python - 如何向包含特定值的行添加新列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50876470/

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