Try the following:
fin = csv.reader(open('LogFile.txt', 'rb'), delimiter='\t') fout = open('newLogFile.csv', 'w') for row in fin: row[0] = ','.join(row[0].split()) fout.write(','.join(row) + '\n')
This will take a line that looks like this: csv.reader()
:
['2012-03-16 13:47:30.465 -0400', 'START', 'Running', 'Lab.script', '19 ', 'on_the']
And then change the first element so that it looks like this:
['2012-03-16,13:47:30.465,-0400', 'START', 'Running', 'Lab.script', '19 ', 'on_the']
And after ','.join()
in the line, you get a line that will be written to your output file:
'2012-03-16,13:47:30.465,-0400,START,Running,Lab.script,19,on_the'
If they have other elements that may contain spaces, and you want to treat them as a separator in the csv output, you can do the following:
fin = csv.reader(open('LogFile.txt', 'rb'), delimiter='\t') fout = open('newLogFile.csv', 'w') for row in fin: fout.write(','.join(','.join(item.split()) for item in row) + '\n')