12

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

user1186173
  • 565
  • 3
  • 7
  • 26

2 Answers2

4

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
Community
  • 1
  • 1
John
  • 13,197
  • 7
  • 51
  • 101
  • 1
    Both the answers were good, but yours solved my second issue where I couldn't read the file as csv.reader() after I configured it originally. Thanks – user1186173 Mar 19 '12 at 13:49
2

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')
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 1
    Hi, I was trying to use csv.reader() after the whole writing process, and everytime I do "for row in csv.reader(): print row[some value]", it will only print for row[0], otherwise it gives me an error saying the list is out of range. I also set the delimiter to "\t", and it still gave me that error.. – user1186173 Mar 19 '12 at 13:41