Python Forum

Full Version: Txt File to CSV
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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.)
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