Skip to content Skip to sidebar Skip to footer

Start Reading And Writing On Specific Line On Csv With Python

I have a CSV file that looks like this: COL_A,COL_B 12345,A=1$B=2$C=3$ How do I read that file and wrote it back to a new file but just the second row (line)? I want the output f

Solution 1:

The following reads your csv, extracts the second row, then writes that second row to another file.

withopen('file.csv') as file:
    second_line = list(file)[1]

withopen('out.csv', mode = 'w') as file:
    file.write(second_line)

Solution 2:

outfile = open(outfilename,"w")
withopen(filename) as f:
    for line in f:
       print >> outfile , line.split()[-1]
outfile.close()

as long as the lines actually look like the line you posted in the OP

Post a Comment for "Start Reading And Writing On Specific Line On Csv With Python"