9

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 this:

import csv

f = open ("file.txt","r")
with open("watout.csv", "w") as output:
    for line in f :
       c.writerows(line)

It is always writes on the first column. How could I resolve this? Thanks.

Zac
  • 2,180
  • 2
  • 23
  • 36
Laith
  • 241
  • 1
  • 2
  • 7
  • 1
    1) That code won't work and 2) You should explain in plain English what you're trying to achieve, as at the moment - it's unclear as to what you're asking/attemping – Jon Clements May 22 '13 at 15:29
  • 3
    You don't seem to be using the csv module, even though you're importing it, and `c` isn't defined. Post a minimal, working example that clearly demonstrates your problem. – Henry Keiter May 22 '13 at 15:30

1 Answers1

14

This is how I solved the problem

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 users replace

with open('out.csv', 'w',newline="") as f:

by

with open('out.csv', 'wb') as f:
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Laith
  • 241
  • 1
  • 2
  • 7