csv 替换文本中的数据

baubqpgj  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(95)

我有多行用逗号分隔的数据,如下所示:

110,2024010,37345,5435493,0,"1234","431","asdf||asdf|adf","Gdańsk||rwet|ertwe ewrtw",0,1444476,"value1","","","51"
110,2024010,70014,5434593,0,"1234","431","asdf||asdf|adf","Kraków||rwet|ertwe ewrtw",0,19444476,"value2","","","51"

我想把每行第11和第12个逗号之间的所有“valueX”替换为相同的值。
Value1和Value2包含了很多对另一个团队很重要的不需要的数据(例如用户名),我们希望保持简单,在每个导入中使用相同的值,以使排序更容易。
我希望将Value1和Value2替换为新的Value3
我使用CSV库:导入csv并测试pandas模块

mzaanser

mzaanser1#

使用pandas

col_position = 11
new_value = 'Value3'

(pd.read_csv('input_csv', header=None)
   .rename(columns=str)
   .assign(**{str(col_position): new_value})
   .to_csv('output_csv', header=None, index=False)
)

在纯python中使用csv模块:

import csv

col_position = 11
new_value = 'Value3'

with open('input_csv') as f_in, open('output_csv', 'w') as f_out:
    w = csv.writer(f_out, delimiter=',')
    for row in csv.reader(f_in, delimiter=','):
        row[col_position] = new_value
        w.writerow(row)

输出文件:

110,2024010,37345,5435493,0,1234,431,asdf||asdf|adf,Gdańsk||rwet|ertwe ewrtw,0,1444476,Value3,,,51
110,2024010,70014,5434593,0,1234,431,asdf||asdf|adf,Kraków||rwet|ertwe ewrtw,0,19444476,Value3,,,51
9w11ddsr

9w11ddsr2#

正如你在上面的回复中所说的,该文件不是csv文件,所以你可以尝试下面的方法使用csv库。

import csv

input_file = 'input.csv'
output_file = 'output.csv'

new_value = "Value"

with open(input_file, 'r', newline='') as infile, open(output_file, 'w', newline='') as outfile:
    reader = csv.reader(infile)
    writer = csv.writer(outfile)

    for row in reader:
        if len(row) >= 12:
            # Replace "Value1" and "Value2" with "Value3" in the 12th field
            row[11] = new_value if row[11] in ["value1", "value2"] else row[11]
        writer.writerow(row)

print("Replacement complete. Updated data saved to output.csv.")

在这里,您的文件将被保存为CSV文件。

相关问题