Parsing a pipe delimited file in python

天涯浪子 提交于 2019-12-17 18:12:53

问题


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.


回答1:


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

EDIT: A more robust way to parse tabular data would be to use the csv library as mentioned below.




回答2:


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



回答3:


import pandas as pd

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

This will store the file in 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 111047 rows.



来源:https://stackoverflow.com/questions/15956169/parsing-a-pipe-delimited-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!