3kstc
3kstc

Reputation: 1956

How to write into a CSV file with Python

Background:

I have a CSV (csv_dump) file with data from a MySQL table. I want copy some of the lines that meet certain conditions (row[1] == condition_1 and row[2] == condition_2) into a temporary CSV file (csv_temp).

Code Snippet:

f_reader = open(csv_dump, 'r')
f_writer = open(csv_temp, 'w')
temp_file = csv.writer(f_writer)

lines_in_csv = csv.reader(f_reader, delimiter=',', skipinitialspace=False)

for row in lines_in_csv:

    if row[1] == condition_1 and row[2] == condition_2:
        temp_file.writerow(row)

f_reader.close()
f_writer.close()

Question:

How can I copy the line that is being read copy it "as is" into the temp file with Python3?

Upvotes: 0

Views: 932

Answers (2)

Mr. A
Mr. A

Reputation: 1231

test.csv

data1,data2,data3
120,80,200
140,50,210
170,100,250
150,70,300
180,120,280

The code goes here

import csv
with open("test.csv", 'r') as incsvfile:
  input_csv = csv.reader(incsvfile, delimiter=',', skipinitialspace=False)
  with open('tempfile.csv', 'w', newline='') as outcsvfile:
    spamwriter = csv.writer(outcsvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    first_row = next(input_csv)
    spamwriter.writerow(first_row)
    for row in input_csv:
        if int(row[1]) != 80 and int(row[2]) != 300:
          spamwriter.writerow(row)

output tempfile.csv

data1,data2,data3
140,50,210
170,100,250
180,120,280

if you don't have title remove these two lines

    first_row = next(input_csv)
    spamwriter.writerow(first_row)

Upvotes: 1

jdpipe
jdpipe

Reputation: 232

The following Python script seems to do the job... However, having said that, you should probably be using a MySQL query to do this work directly, instead of re-processing from an intermediate CSV file. But I guess there must be some good reason for wanting to do that?

mycsv.csv:

aa,1,2,5
bb,2,3,5
cc,ddd,3,3
hh,,3,1
as,hfd,3,3

readwrite.py:

import csv

with open('mycsv.csv', 'rb') as infile:
    with open('out.csv', 'wb') as outfile:
        inreader = csv.reader(infile, delimiter=',', quotechar='"')
        outwriter = csv.writer(outfile)
        for row in inreader:
            if row[2]==row[3]:
                outwriter.writerow(row)

out.csv:

cc,ddd,3,3
as,hfd,3,3

With a little more work, you could change the 'csv.writer' to make use of the same delimiters and escape/quote characters as the 'csv.reader'. It's not exactly the same as writing out the raw line from the file, but I think it will be practically as fast since the lines in question have already clearly been parsed without error if we have been able to check the value of specific fields.

Upvotes: 0

Related Questions