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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With