0

I'm new to python but I'm having trouble reading a text file which contains data separated by "|" as the delimiter. How would I separate the file into columns in a CSV format.

import csv
my_file_name = "NVG.txt"
cleaned_file = "cleanNVG.csv"

with open(my_file_name, 'r') as infile, open(cleaned_file, 'w') as outfile:
    data = infile.read()

    data = data.replace("|","")
    outfile.write(data)

This code gets rid of the | to a blank but all the data is just in one column now. How can I format this correctly? I appreciate your help in advance.

1
  • 2
    Why are you removing the |? Why don't you just tell csv that that is your separator? i.e. csv.reader(infile, delimiter='|') Commented Oct 4, 2016 at 14:10

3 Answers 3

5

The csv module allows you to read csv files with practically arbitrary delimiters.

with open(my_file_name, 'r', newline='') as infile:
    for line in csv.reader(infile, delimiter='|'):
        # do stuff

If you really want to reformat the file, you can use the csv.writer directly:

with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w', newline='') as outfile:
    writer = csv.writer(outfile)
    for line in csv.reader(infile, delimiter='|'):
        writer.writerow(line)

Note that your approach doesn't work because you remove the separator instead of replacing it. data.replace("|","") will replace each | with the empty string, i.e. "foo|bar" becomes "foobar". You must replace the old separator with a new one, e.g. data.replace("|", ",").

Sign up to request clarification or add additional context in comments.

3 Comments

Ohh Okay!!! Thanks! The data is now in their columns but it leaves an empty row after. so Row 1 has data and Row 2 is blank , Row 3 has Data and Row 4 is blank and so on.... why is it doing that?
@Cesar Define the writer object as writer = csv.writer(outfile, lineterminator="\n")
Actually never mind, the **newline= ''**was messed up. This worked perfect!!
1

The simplest way with your code would be to replace "|" with "," rather than removing "|"

data = data.replace("|", ",")

Comments

1

You're importing the csv module, but aren't using it. Make use of csv.reader

with open(my_file_name, 'r') as infile, open(cleaned_file, 'w') as outfile:
    reader = csv.reader(infile, delimiter='|')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.