John Doe
John Doe

Reputation: 321

Parsing a pipe-delimited file in Python

I'm trying to parse a pipe-delimited file and pass the values into a list, so that later I can print selective values from the list.

The file looks like:

name|age|address|phone|||||||||||..etc

It has more than 100 columns.

Upvotes: 22

Views: 86766

Answers (4)

Alexis
Alexis

Reputation: 61

In 2022, with Python 3.8 or above, you can simply do:

import csv

with open(file_path, "r") as csvfile:
    reader = csv.reader(csvfile, delimiter='|')
    for row in reader:
        print(row[0], row[1])

Upvotes: 3

manjusha
manjusha

Reputation: 320

Use Pandas:

import pandas as pd

pd.read_csv(filename, sep="|")

This will store the file in a dataframe. For each column, you can apply conditions to select the required values to print. It takes a very short time to execute. I tried with 111,047 rows.

Upvotes: 22

OdinX
OdinX

Reputation: 4211

If you're parsing a very simple file that won't contain any | characters in the actual field values, you can use split:

fileHandle = open('file', 'r')

for line in fileHandle:
    fields = line.split('|')

    print(fields[0]) # prints the first fields value
    print(fields[1]) # prints the second fields value

fileHandle.close()

A more robust way to parse tabular data would be to use the csv library as mentioned in Spencer Rathbun's answer.

Upvotes: 20

Spencer Rathbun
Spencer Rathbun

Reputation: 14910

Use the 'csv' library.

First, register your dialect:

import csv
csv.register_dialect('piper', delimiter='|', quoting=csv.QUOTE_NONE)

Then, use your dialect on the file:

with open(myfile, "rb") as csvfile:
    for row in csv.DictReader(csvfile, dialect='piper'):
        print row['name']

Upvotes: 43

Related Questions