Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Txt File to CSV
#1
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.
Larz60+ write Dec-07-2020, 09:40 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time, please use bbcode tags on future posts.
Reply
#2
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.)
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020