I have a large text file in the below format, which I wish to convert into a CSV File. The column names on the CSV file should corrospond to the first part of the tuples seen below. Its safe to assume the first item in the line, which is not a tuple, will always be in the below format.
Other issues include each line may not have the same fields – some have for example, Statuses, some dont. Some have multiple instances of the same field, in which case I require the second part of the tuples to be concatenated (eg To Mr Smith; Mrs Green) but these are issues which are further away for now.
[' Message 1 '];['Status', 'Read'];['Message ID', '012434'];['Message Truncation', 'OK'];['Priority', 'Low'];['Sent Time', '15/12/2010 05:56:36']
[' Message 2 '];['ColumnName', 'Read'];['ColumnName2', '012434'];['Message Truncation', 'OK'];['Priority', 'Low'];['Sent Time', '15/12/2010 05:56:36']
[' Message 3 '];['To', 'Mr Smith'];['To', 'Mrs green'];['Message Truncation', 'OK'];['Priority', 'Low'];['Sent Time', '15/12/2013 05:56:36']
…
My plan is to iterate thru every block in the file to establish the column names, then start adding data to these column names, leaving blanks when appropriate. Im just wonderint how to go about this in a pythonic manner, as I’ve played around with a list of dictionaries and got stuck.
I think I need to split the line, then add each tuple to a dictionary. Any help?
Thanks!
for line in file:
line_split = line.split(';')
Solution using pure python…
Input:
Output:
Update
This handles multiple entries with same type and join then with
":".