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?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
elyon
  • 181
  • 2
  • 6
  • 16
  • Related: [how to convert tab separated pipe separated to csv file format](http://stackoverflow.com/questions/1366775/how-to-convert-tab-separated-pipe-separated-to-csv-file-format-in-python) – fredtantini Dec 18 '14 at 16:34

5 Answers5

11

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)
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
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)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • @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 Oct 02 '15 at 15:37
  • 1
    @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. – martineau Oct 02 '15 at 15:50
  • Returns an error in Python 3.6.4: `_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)` – Stevoisiak Sep 19 '18 at 15:31
  • @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](https://docs.python.org/3/library/csv.html#module-csv)). – martineau Sep 19 '18 at 15:34
  • @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) – martineau Sep 19 '18 at 16:01
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.

Jameson Welch
  • 31
  • 1
  • 4
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
Raghavendra Gupta
  • 355
  • 1
  • 2
  • 15
-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.

Gullydwarf
  • 345
  • 3
  • 15