1

I want to convert a comma-delimited CSV file to a pipe-delimited file with Python:

This is how I am reading my csv file:

with open('C://Path//InputFile.csv') as fOpen:
    reader     =     csv.DictReader(fOpen)

    for row in reader:
        for (k, v) in row.items():
            columns[k].append(v)

c = csv.writer(open("C://Path//OutputFile.txt","wb"), delimiter="|")

How would I write it as pipe delimited file?

1

5 Answers 5

12

Adapting martineau's answer to fix newline issues in Python 3.

import csv

with open('C:/Path/InputFile.csv') as fin:
    # newline='' prevents extra newlines when using Python 3 on Windows
    # https://stackoverflow.com/a/3348664/3357935
    with open('C:/Path/OutputFile.txt', 'w', newline='') as fout:
        reader = csv.DictReader(fin, delimiter=',')
        writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
        writer.writeheader()
        writer.writerows(reader)
Sign up to request clarification or add additional context in comments.

Comments

6

This does what I think you want:

import csv

with open('C:/Path/InputFile.csv', 'rb') as fin, \
     open('C:/Path/OutputFile.txt', 'wb') as fout:
    reader = csv.DictReader(fin)
    writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
    writer.writeheader()
    writer.writerows(reader)

5 Comments

@martineau The above code will not work in python 2.6, you should mention the python version. It will show syntax error for opening two files like that. If possible please provide the solution for 2.6 as well.
@cyborg: Just nest (indent) the second with statement and the lines following it all one level inside the first. Apparently having multiple context expressions wasn't added until Python 2.7.
Returns an error in Python 3.6.4: _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
@StevenVascellaro: This code was written for Python 2.x, so doesn't open() the files the way they should be for Python 3 (as shown in the Py3 csv module's documentation).
@Steven: Both files should be opened in the same way as shown in the examples in the linked Python 3 csv module's documentation. (i.e. not in binary mode)
2

I found a quick way to change the comma delimiter to a pipe with pandas. When I converted my dataframe to a csv using "|" as delimiter:

df.to_csv(fileName, sep="|")

I don't have much experience with the csv module so if these solutions aren't interchangeable then someone might have to chime in. But this worked surprisingly well for me.

Comments

0

You can use pandas to achieve the conversion of csv to pipe-delimited (or desired delimited) file.

import pandas as pd
df = pd.read_csv(r'C:\Users\gupta\Documents\inputfile.csv') #read inputfile in a dataframe
df.to_csv(r'C:\Users\gupta\Desktop\outputfile.txt', sep = '|', index=False) #write dataframe df to the outputfile with pipe delimited

Comments

-1

https://docs.python.org/2/library/csv.html for Python 2.x https://docs.python.org/3.3/library/csv.html for Python 3.x

These pages explain how to use csv.writer.

Without testing it, your code looks syntacticly valid. All you need to do is add some c.writerow('data','here') to write your data.

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.