在python中解析管道分隔文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15956169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing a pipe delimited file in python
提问by John Doe
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.
它有 100 多列。
采纳答案by vimist
If you're parsing a very simple file that won't contain any |characters in the actual field values, you can use split:
如果您正在解析一个非常简单的文件,该文件|在实际字段值中不包含任何字符,您可以使用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 csvlibrary as mentioned below.
编辑:更可靠的方法来分析表格数据将使用csv库作为下面提到。
回答by Spencer Rathbun
Use the csv library.
使用csv 库。
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']
回答by manjusha
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.
这会将文件存储在数据框中。对于每一列,您可以应用条件来选择要打印的所需值。执行时间很短。我尝试了 111047 行。

