Gautham Kanthasamy
Gautham Kanthasamy

Reputation: 229

Write from Text files to CSV Files

I am trying to write all the rows that contain the string: from a bunch of Text files. This is my code:

import os
import glob
import csv
import re

#Defining Keyword
keyword = '2012-07-02'

#Code to merge all relevant LOG files into one file and insert 
with open('Combined-01022012.txt' , 'w', newline = '') as combined_file:
    csv_output = csv.writer(combined_file)
    for filename in glob.glob('FAO_Agg_2012_Part_*.txt'):
        with open(filename, 'rt', newline = '') as f_input:
        #with gzip.open((filename.split('.')[0]) + '.gz', 'rt', newline='') as f_input:
            csv_input = csv.reader(f_input)
            for row in csv_input:
                row.insert(0, os.path.basename(filename))
                try:
                    if keyword in row[2]:
                        csv_output.writerow(row)
                #row.insert(0, os.path.basename(filename))
                #csv_output.writerow(row)
                except:
                    continue
            continue

Everything seems to be right and the code runs but nothing gets written on to my text file. What could be going wrong?

Upvotes: 1

Views: 53

Answers (1)

zwer
zwer

Reputation: 25789

Your main problem is in the lines:

row.insert(0, os.path.basename(filename))
try:
    if keyword in row[0]:
        csv_output.writerow(row)
except:
    continue

You're essentially inserting your parent folder name of the current file as the first entry of your row, and then on the very next line you're checking if that entry (row[0]) contains your keyword. Unless the parent folder contains your keyword (2012-07-02) that condition will never evaluate as True. I'd mix this up as:

if keyword in row[0]:
    csv_output.writerow([os.path.basename(filename)] + row)

Also, using blank except is a very, very bad idea. If you're looking to capture a specific exception, define it in your except clause.

Upvotes: 2

Related Questions