简体   繁体   English

解析 Python 中的竖线分隔文件

[英]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.它有 100 多个列。

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

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 csv library as mentioned below . 编辑:解析表格数据的更可靠的方法是使用csv库, 如下所述

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行。

In 2022, with Python 3.8 or above, you can simply do:在 2022 年,使用 Python 3.8 或更高版本,您可以简单地执行以下操作:

import csv

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用管道分隔的平面文件插入python以供Pandas和Stats使用 - Piping a pipe-delimited flat file into python for use in Pandas and Stats 将由竖线(竖线分隔)分隔的格式化数据保存到 Python 中的文件中 - Saving formatted data separated by vertical bars(pipe-delimited) to a file in Python 在 Python 中将管道分隔的文本文件文件夹转换为 CSV - Convert a folder of pipe-delimited text files to CSV in Python 如何读取以竖线分隔的交易文件并生成销售报告 - How can I read a pipe-delimited transaction file and generate the sales reports 使用 Pandas 将带有 html 标签的管道分隔文件读取到数据帧中 - Reading pipe-delimited file with html tags using Pandas into data-frame 根据Pandas中以竖线分隔的列创建多个新列 - Create Multiple New Columns Based on Pipe-Delimited Column in Pandas 根据熊猫中以竖线分隔的列创建多个新行 - Create Multiple New rows Based on Pipe-Delimited Column in Pandas 在 Python 中将 csv 文件转换为管道分隔文件 - Convert csv file to pipe delimited file in Python 解析 pipe 分隔 json output - Parsing pipe delimited json output 拆分管道分隔的系列,按单独的系列分组,并在新列中返回每个拆分值的计数 - Split a pipe-delimited series, groupby a separate series, and return the counts of each split value in new columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM