在python中解析管道分隔文件 [英] Parsing a pipe delimited file in python
本文介绍了在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.
文件如下:
name|age|address|phone|||||||||||..etc
它有超过100列.
推荐答案
如果要解析的非常简单的文件在实际字段值中不包含任何|
字符,则可以使用
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()
编辑:一种更强大的分析表格数据的方法是将csv
库用作在下面提到.
A more robust way to parse tabular data would be to use the csv
library as mentioned below.
这篇关于在python中解析管道分隔文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文