Python Forum
Txt File to CSV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Txt File to CSV (/thread-31373.html)



Txt File to CSV - Andie19 - Dec-07-2020

Hi
Hoping someone can help. I am trying to convert a txt file (hundred of lines)- (sample below)
pc_account = An account is an entity (person or company) that applies for or purchases one or more policies from the carrier. Account attributes include years in business, industry code (SIC, NAICS, etc)

By splitting the file into an CSV file that parses out the file into columns by the '=' exist

I have something like this:

import csv

with open('S:\descriptions.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split("=") for line in stripped if line)
    with open('S:\log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'Defn'))
        writer.writerows(lines)
Any help is appreciated
thanks in advance for your time.


RE: Txt File to CSV - bowlofred - Dec-07-2020

Please put python code inside python tags (use the python logo button above the editor window).

What do you need assistance with? Is it working? If it's not working, what is happening? (show complete error messages, show how the output differs from your expectation. Show sample input.)


RE: Txt File to CSV - Andie19 - Dec-08-2020

thank you for the reply. New to the forum and very new to writing the code, that's all I had....
as result of running that code I am receiving error SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: malformed \N character escape.

thanks