user3130236
user3130236

Reputation: 71

selecting specific rows from an unstructured csv file and writing to another file using python

I am trying to iterate through an unstrucutred csv file (it has no specific headings). The file is generated by an instrument. I would need to select specific rows that have specific column values and create another file. Below is the example of the file layout

  ,success, (row1)

1,2,protocol (row2)

78,f14,34(row3)

,67,34(row4)

,f14,34(row5)

3,f14,56,56(row6)

I need to select all rows with 'fi4' value. Below is the code

import csv
import sys
reader = csv.reader(open('c:/test_file.csv', newline=''), delimiter=',', quotechar='|')
for row in reader:
      print(','.join(row))

I am unable to go beyond this point.

Upvotes: 1

Views: 473

Answers (2)

martineau
martineau

Reputation: 123393

You just need to check and see whether the row is one you're interested in or not by checking the value of the column and see if it's what you're looking for. That could be done with a simpleif row[1] == 'f14'conditional statement. However that would fail on any blank lines -- which it looks like your input file may have -- so you'd need to preface that check with another to make sure the row had at least that many columns in it.

To create another csv file with just those rows in it, all you'd need to write each row that passed all the checks to another file opened for output -- instead of, or in addition to, printing the row out. Here's a very concise way of just writing the rows to another file.

(Note: I'm not sure why you had thequotechar='|'in your code on thecsv.reader()call because there aren't any quote characters in the input file shown, so I left it out in the code below -- you might need to add it back if indeed that's what it would be if there were any.)

import csv

with open('test_file.csv', newline='') as infile, \
     open('test_file_out.csv', 'w', newline='') as outfile:

    csv.writer(outfile).writerows(row for row in csv.reader(infile)
                                    if len(row) >= 2 and row[1] == 'f14')

Contents of'test_file_out.csv'file afterwards:

78,f14,34(row3)
,f14,34(row5)
3,f14,56,56(row6)

Upvotes: 0

ales_t
ales_t

Reputation: 2017

You're almost there:

for row in reader:
    if row[1] == 'f14':
        print(','.join(row))

Upvotes: 0

Related Questions