Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9099457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:37:24+00:00 2026-06-17T00:37:24+00:00

I have a large text file in the below format, which I wish to

  • 0

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(';')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:37:25+00:00Added an answer on June 17, 2026 at 12:37 am

    Solution using pure python…

    infile = "listdata.txt"
    data = open(infile, "r").readlines()
    
    dataDict = []
    columns = []
    
    # Create a dictionary list
    for line in data:
        row = line.split(";");
        rowData = {}
        for cell in row:
            cell = cell.strip()[1:-1].split(",")
            if len(cell) > 1:
                rowData[cell[0].strip().strip('"').strip("'")] = cell[1].strip().strip('"').strip("'")
        keys = rowData.keys()
        dataDict.append(rowData)
        columns = list(set(columns) | set(keys))
    
    # Write dictionary list to file
    outfile = "listdata.csv"
    fp = open(outfile, "w")
    
    for key in columns:
        fp.write(key + ", ")
    
    fp.write("\n")
    
    for data in dataDict:
        for key in columns:
            if key in data:
                fp.write(data[key] + ",")
            else:
                fp.write(",")
        fp.write("\n")
    
    fp.close()
    

    Input:

    [' 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']
    

    Output:

    Status, Sent Time, To, ColumnName2, Message ID, Message Truncation, Priority, ColumnName, 
    Read,15/12/2010 05:56:36,,,012434,OK,Low,,
    ,15/12/2010 05:56:36,,012434,,OK,Low,Read,
    ,15/12/2013 05:56:36,Mrs green,,,OK,Low,,
    

    Update

    This handles multiple entries with same type and join then with ":".

    key = cell[0].strip().strip('"').strip("'")
    value = cell[1].strip().strip('"').strip("'")
    if key in rowData:
        rowData[key] = rowData[key] + ":" + value
    else:
        rowData[key] = value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iPad application I have a large text file which I want to divide into
I have a large text file in the format of. english word: spanish equivalent
I have a large text file as below imported in MATLAB : Run Lat
I have a large text files in the format shown below - ID SNP
I have a large text file I am reading from and I need to
I have a a large text file (over 70mb) and need to count the
I have to remove quotes from a relatively large text file. I have looked
Okay, basically, I have a large list of phone numbers in a text file
I have a large textfile, which has linebreaks at column 80 due to console
I have several large text files (30m+ lines, >1GB) which are being processed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.