当单词之间用"|"分隔时,如何读取文件? (PSV)? [英] How to read file when the words are separated by "|" (PSV)?

查看:123
本文介绍了当单词之间用"|"分隔时,如何读取文件? (PSV)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我有一个文件,单词之间用|分隔,例如:city|state|zipcode.我的文件阅读器无法分隔单词.另外,我希望我的文件阅读器从第2行开始,而不是从第1行开始.如何让我的文件阅读器将单词分开?

In Python, I have a file which the words are separated by |, for example: city|state|zipcode. My file reader is unable to separate the words. Also, I want my file reader to start on line 2 rather than line 1. How do I get my file reader to separate the words?

import os
import sys

def file_reader(path, num_fields, seperator = ',', header = False):
    try:
        fp = open(path, "r", encoding="utf-8")
    except FileNotFoundError:
        raise FileNotFoundError("Unable to open file.")
    else:
        with fp:
            for n, line in enumerate(fp, 1):
                fields = line.rstrip('/n').split(seperator)
                if len(fields) != num_fields:
                    raise ValueError("Unable to read file.")
                elif n == 1 and header:
                    continue
                else:
                    yield tuple([f.strip() for f in fields])

推荐答案

如果您不介意使用现有框架,则可以使用pandas.您可以使用skiprows = 1跳过第一行,并使用sep ='|'

If you don't mind to use existing framework, you can use pandas. You can skip first row using skiprows=1 and change the separator using sep='|'

# load pandas
import pandas as pd

# read file as pandas dataframe
dataframe = pd.read_csv(file,skiprows=1,sep='|')
print(dataframe)

要安装熊猫

pip install pandas

read_csv的熊猫文档

Pandas documentation for read_csv

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

其他选项是使用csv阅读器读取您的psv文件

Other option is to use csv reader to read your psv file

import csv

with open('file.psv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='|')
    next(csv_reader, None)  # read once to skip the header once

    for row in csv_reader:
            print(row)

这篇关于当单词之间用"|"分隔时,如何读取文件? (PSV)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆