第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Python 從包含多組數(shù)據(jù)的文件中讀取指定列

使用 Python 從包含多組數(shù)據(jù)的文件中讀取指定列

胡說叔叔 2021-06-07 10:09:05
我正在嘗試使用 python 從 .txt 文件加載多列數(shù)據(jù)。我的文件包含多組數(shù)據(jù),每組都有一個標(biāo)題。我想選擇一個集合,然后從中選擇 2 列。我正在使用 genfromtxt 讀取 .txt 文件,但它將集合的標(biāo)題作為列讀取,因此它給了我這種錯誤:第 2 行(有 4 列而不是 1 列)這是我的 txt 文件的示例,其中 TC_14TeV_NLO 和 TC_13TeV_LO 是標(biāo)題,我想取每組的前 2 列:TC_14TeV_NLO 1000 1.51100e+01 6.2e-03 4.1e-02%2000 7.36556e-01 4.4e-04 5.9e-02%3000 7.85092e-02 5.1e-05 6.5e-02%4000 1.17810e-02 7.4e-06 6.3e-02%5000 2.39873e-03 1.3e-06 5.2e-02%6000 7.18132e-04 2.7e-07 3.7e-02%7000 3.10281e-04 8.1e-08 2.6e-02%8000 1.67493e-04 3.3e-08 1.9e-02%9000 1.01369e-04 2.2e-08 2.2e-02%10000 6.54776e-05 1.6e-08 2.4e-02%TC_13TeV_LO1000 1.04906e+01 1.7e-03 1.7e-02%2000 4.53170e-01 8.1e-05 1.8e-02%3000 4.25722e-02 7.9e-06 1.9e-02%4000 5.80036e-03 1.1e-06 1.9e-02%5000 1.17278e-03 2.1e-07 1.8e-02%6000 3.82330e-04 6.1e-08 1.6e-02%7000 1.78036e-04 2.7e-08 1.5e-02%8000 9.91945e-05 1.9e-08 1.9e-02%9000 6.05766e-05 1.6e-08 2.6e-02%10000 3.92631e-05 1.2e-08 3.0e-02%
查看完整描述

2 回答

?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊

首先,定義一個函數(shù)將文件拆分為多個部分。這是一個生成器,它生成一系列行列表:


def split_sections(infile):

    """Generate a sequence of lists of lines from infile delimited by blank lines.

    """

    section = []

    for line in infile:

        if not line.strip():

            if section:

                yield section

                section = []

        else:

            section.append(line)

    if section: # last section may not have blank line after it

        yield section

那么你的實際任務(wù)相當(dāng)簡單:


with open(path) as infile:

    for lines in split_sections(infile):

        heading = lines[0].rstrip()

        data = np.genfromtxt(lines[1:], usecols=[0,1])

        print(heading)

        print(data)


查看完整回答
反對 回復(fù) 2021-06-08
  • 2 回答
  • 0 關(guān)注
  • 417 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號