Parsing a pipe-delimited file in Python

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']

Leave a Comment