*.csv to *.txt after adding columns
Dave Angel
davea at davea.name
Tue Sep 17 22:18:46 EDT 2013
On 17/9/2013 21:42, Bryan Britten wrote:
> Hey, gang, I've got a problem here that I'm sure a handful of you will know how to solve. I've got about 6 *.csv files that I am trying to open; change the header names (to get rid of spaces); add two new columns, which are just the results of a string.split() command; drop the column I just split; and then finally export to *.txt files. Here's the code I'm using:
>
> import os
> import csv
>
>
> fileHandle = 'Path/To/Data'
> varNames = 'ID\tCaseNum\tDate\tTime\tBlock\tIUCR\tPrimaryType\tDescription\tLocDesc\tArrest\tDomestic\tBeat\tDistrict\tWard\tCommArea\tFBICode\tXCoord\tYCoord\tYear\tUpdatedOn\tLat\tLong\tLoc\n'
>
> for csvFile in os.listdir(fileHandle):
> outFile = open(fileHandle + os.path.splitext(csvFile)[0] + '.txt', 'w')
> inFile = open(fileHandle + csvFile, 'rb')
> reader = csv.reader(inFile, delimiter=',')
> rowNum = 0
> for row in reader:
> if rowNum == 0:
> outFile.write(varNames)
> rowNum += 1
> else:
> date, time = row[2].split()
> row.insert(3, date)
> row.insert(4, time)
> row.remove(row[2])
> outFile.write('\t'.join(row) + '\n')
> outFile.close()
> inFile.close()
>
>
> The issue I'm having is that the *.txt files I'm generating are empty. I assume some unraised error is being thrown,
How can you NOT know if an exception is being raised? How are you
running the code, in a terminal window? Can't you just see what gets
printed (as stderr)?
> but I'm new to Python and am self taught, so I don't know where to look
> or how to troubleshoot.
>
> When I run the code on just one file and print the output instead of writing it, it looks exactly like what I'd want. So I'm at a loss for where the problem is.
You describe two changes, but don't show them. How about you do them
one at a time, or perhaps even better, add prints so it both does the
file(s) AND prints the output?
What I think is happening is that you're missing the path separator
between the "Path/To/Data" and the basename. You should be combining
those with os.path.join(), not with +
For a quick & dirty check, add a trailing slash to the fileHandle. But
that's not the right way to fix it.
--
DaveA
More information about the Python-list
mailing list