如何仅从Python中的特定模式开始读取csv文件中的行? [英] How can I read lines in csv file only starting with a certain pattern in Python?

查看:109
本文介绍了如何仅从Python中的特定模式开始读取csv文件中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有几个表示某些数据的csv文件,每个文件可能都有不同的初始注释行

So I have several csv files that represent some data, each of which may have different lines of initial comments

table_doi: 10.17182/hepdata.52402.v1/t7
name: Table 7
...
ABS(YRAP), < 0.1
SQRT(S) [GeV], 1960
PT [GEV], PT [GEV] LOW, PT [GEV] HIGH, D2(SIG)/DYRAP/DPT [NB/GEV]
67, 62, 72, 6.68
...
613.5, 527, 700, 1.81E-07

我只想读入相关数据及其标头,它们从行开始

I would like to read in only the relevant data and their headers as well, which start from the line

PT [GEV], PT [GEV] LOW, PT [GEV] HIGH, D2(SIG)/DYRAP/DPT [NB/GEV]

因此,我想到的策略是找到模式PT [GEV]并从那里开始读取.

Therefore the strategy I would think of is to find the pattern PT [GEV] and start reading from there.

但是,我不确定如何在Python中实现此目标,有人可以帮我吗?

However, I am not sure how to achieve this in Python, could anyone help me on that?

提前谢谢!

顺便说一句,我目前拥有的功能是

By the way, the function I currently have is

import os
import glob
import csv

def read_multicolumn_csv_files_into_dictionary(folderpath, dictionary):
    filepath = folderpath + '*.csv'
    files = sorted(glob.glob(filepath))
    for file in files:
        data_set = file.replace(folderpath, '').replace('.csv', '')
        dictionary[data_set] = {}
        with open(file, 'r') as data_file:
            data_pipe = csv.DictReader(data_file)
            dictionary[data_set]['pt'] = []
            dictionary[data_set]['sigma'] = []
            for row in data_pipe:
                dictionary[data_set]['pt'].append(float(row['PT [GEV]']))
                dictionary[data_set]['sigma'].append(float(row['D2(SIG)/DYRAP/DPT [NB/GEV]']))
    return dictionary

仅在我手动删除csv文件中的初始注释时有效.

which only works if I manually delete those initial comments in the csv files.

推荐答案

假定每个文件都有以PT [GEV]开头的行:

Assuming every file has a line that startswith PT [GEV]:

import os
import pandas as pd

...
csvs = []
for file in files:
    with open(file) as f:
        for i, l in enumerate(f):
            if l.startswith('PT [GEV]'):
                csvs.append(pd.read_csv(file, skiprows = i))
                break
df = pd.concat(csvs)

这篇关于如何仅从Python中的特定模式开始读取csv文件中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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