Reputation: 565
I have multiple lines of texts in a text file that look similar to this:
2012-03-16 13:47:30.465 -0400 START Running Lab.script 19 on_the
I want to be able to convert this text file into csv. I've already done that using this code:
fin = csv.reader(open('LogFile.txt', 'rb'), delimiter='\t')
fout = open('newLogFile.csv', 'w')
for row in fin:
fout.write(','.join(row) + '\n')
But now, my issue is that I need to be able to add a "," after the spaces in this part of the line:
2012-03-16 13:47:30.465 -0400
I'm not sure how to do it, I've tried using split(), to split the current row/position but it didn't work. Any suggestions would be very helpful.
Thank you
Upvotes: 12
Views: 24427
Reputation: 13699
Would helpful to instead just tab delimit everything from the beginning? If so you can refer to this answer, essentially
There is a special-case shortcut for exactly this use case!
If you call str.split without an argument, it splits on runs of whitespace instead of single characters. So:
>>> ' '.join("Please \n don't \t hurt \x0b me.".split()) "Please don't hurt me."
so for you it would be
newLogFile = open('newLogFile.csv', 'w')
textFile = open('LogFile.txt', 'rb')
for row in textFile:
newLogFile.write('\t'.join(row.split()))
Also you said
But now, my issue is that I need to be able to add a "," after the spaces in this part of the line:
2012-03-16 13:47:30.465 -0400
to me that sounds like you want
2012-03-16 ,13:47:30.465 ,-0400
Upvotes: 4
Reputation: 208405
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 row that looks like this after being read in by 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()
on the row you get the 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 there are other elements that may have spaces in them and you want to treat them all as a delimiter in your output csv, 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')
Upvotes: 2