Replace data in csv file using python

后端 未结 2 1567
傲寒
傲寒 2020-12-18 15:29

This is the new input file format. I need to automate the process of replacing the content of one column in a .csv file with the use of python. I can also open the .csv file

2条回答
  •  一生所求
    2020-12-18 15:58

    You could use the csv module in Python to achieve this.

    csv.reader will return each row as a list of strings. You could then use csv.writer to stream each row and modify the ID column at this point, this will create a new file though.

    So:

    import csv
    reader = csv.reader(open('file.csv', 'rb'))
    writer = csv.writer(open('outfile.csv','wb'))
    for row in reader:
        writer.writerow([row[0], "input", row[2], row[3]])
    

提交回复
热议问题