Use Python to write on specific columns in csv file

前端 未结 2 958
萌比男神i
萌比男神i 2021-02-04 17:06

I have data in a file and I need to write it to CSV file in specific column. The data in file is like this:

002100
002077
002147

My code is th

2条回答
  •  无人共我
    2021-02-04 17:34

    This is how I solved the problem

    f1 = open ("inFile","r") # open input file for reading
    
    with open('out.csv', 'wb') as f: # 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()   
    

提交回复
热议问题