Skip to content Skip to sidebar Skip to footer

Convert Csv File To Pipe Delimited File In Python

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

Solution 1:

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

import csv

withopen('C:/Path/InputFile.csv') as fin:
    # newline='' prevents extra newlines whenusing Python 3on Windows
    # https://stackoverflow.com/a/3348664/3357935withopen('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)

Solution 2:

This does what I think you want:

import csv

withopen('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)

Solution 3:

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.

Solution 4:

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

Solution 5:

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.

Post a Comment for "Convert Csv File To Pipe Delimited File In Python"