By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

.txt file to excel .csv

P: 2

I have some very large(several Mb) text files I am trying to process and parse using a Python script (novice python user). I am using the eclipse IDE and version 3 of python if that makes a difference.

I am trying to also make use of the csv module in python.

the .txt file has the following format for data:

Expand|Select|Wrap|Line Numbers
  2. 09:04:11
  3. <break>0M!00021<CR><LF>
  4. 0<CR><LF>
  5. 0D0!0+32044+31681<CR><LF>
  7. 09:04:16
  8. <break>0M!00021<CR><LF>
  9. 0<CR><LF>
  10. 0D0!0+32051+31685<CR><LF>
  12. 09:04:21
  13. <break>0M!00021<CR><LF>
  14. 0<CR><LF>
  15. 0D0!0+32049+31679<CR><LF>
I would like to extract the information into the following CSV format:

Expand|Select|Wrap|Line Numbers
  1. 09:04:11,32044,31681
  2. 09:04:16,32051,31685
  3. 09:04:21,32049,31679
any help or pseudo code would be greatly appreciated.
May 16 '11 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 2.5K+
P: 2,851
Create an open file object for reading
Initialize an empty list to save the data
Iterate on the file object using a for loop
Start a nested for loop (for i in range(5))
Initialize a temporary list
i == 0, strip white space and append current line to temp list
i == 3, split string on "+", strip white space, append elements 1 and 2 to temp list
Advance file position one line by calling file object method next() or readline()
End of nested loop, append temp list to data list
End of file iteration
Close file object
Open file object for writing
Write data to disc
Close file object
May 17 '11 #2

P: 2
Thanks for pseudo code, I got something else implemented but this is a more robust solution.
May 17 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.