.txt file to excel .csv

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Justin C
    New Member
    • May 2011
    • 2

    .txt file to excel .csv

    Hello,

    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:

    Code:
    09:04:11
    <break>0M!00021<CR><LF>
    0<CR><LF>
    0D0!0+32044+31681<CR><LF>
    
    09:04:16
    <break>0M!00021<CR><LF>
    0<CR><LF>
    0D0!0+32051+31685<CR><LF>
    
    09:04:21
    <break>0M!00021<CR><LF>
    0<CR><LF>
    0D0!0+32049+31679<CR><LF>
    I would like to extract the information into the following CSV format:

    Code:
    09:04:11,32044,31681
    09:04:16,32051,31685
    09:04:21,32049,31679
    any help or pseudo code would be greatly appreciated.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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

    Comment

    • Justin C
      New Member
      • May 2011
      • 2

      #3
      Thanks for pseudo code, I got something else implemented but this is a more robust solution.

      Comment

      Working...