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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

讀取換行的分隔文件

讀取換行的分隔文件

慕村225694 2021-06-10 18:08:39
如果對(duì)此已經(jīng)有了明顯的答案,我深表歉意。我有一個(gè)非常大的文件,對(duì)解析提出了一些挑戰(zhàn)。我從我的組織外部收到這些文件,因此我無法更改它們的格式。首先,文件以空格分隔,但表示數(shù)據(jù)“列”的字段可以跨越多行。例如,如果您有一行應(yīng)該是 25 列數(shù)據(jù),它可能會(huì)在文件中寫為:1 2 3 4 5 6 7 8 9 10 11 12 13 14   15 16 17 18 19 20 21   22 23 24 251 2 3 4 5 6 7 8 9 10 11 12 13   14 15 16 17 18  19 20 21 22 23 24 25如您所見,我不能依賴每組數(shù)據(jù)都在同一行上,但我可以依賴每組數(shù)據(jù)的列數(shù)相同。更糟糕的是,該文件遵循一個(gè)定義:數(shù)據(jù)類型格式,其中前 3 行左右將描述數(shù)據(jù)(包括一個(gè)告訴我有多少行的字段),接下來的 N 行是數(shù)據(jù)。然后它會(huì)再次回到 3 行格式來描述下一組數(shù)據(jù)。這意味著我不能只為 N 列格式設(shè)置一個(gè)閱讀器并讓它運(yùn)行到 EOF。我擔(dān)心內(nèi)置的 python 文件讀取功能會(huì)變得非常難看,但我在 csv 或 numpy 中找不到任何有效的東西。有什么建議么?編輯:就像不同解決方案的一個(gè)例子:我們?cè)?MATLAB 中有一個(gè)舊工具,它在打開的文件句柄上使用 textscan 解析這個(gè)文件。我們知道列數(shù),因此我們執(zhí)行以下操作:data = textscan(fid, repmat('%f ',1,n_cols), n_rows, 'delimiter', {' ', '\r', '\n'}, 'multipledelimsasone', true);這將讀取數(shù)據(jù),無論它如何包裝,同時(shí)保持文件句柄打開以稍后處理下一部分。這樣做是因?yàn)槲募?,可能?huì)導(dǎo)致 RAM 使用量過多。
查看完整描述

1 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是您如何進(jìn)行的草圖:(編輯:有一些修改)


file = open("testfile.txt", "r") 


# store data for the different sections here

datasections = list()


while True:

    current_row = []


    # read three lines


    l1 = file.readline()

    if line == '': # or other end condition

        break

    l2 =  file.readline()

    l3 =  file.readline()


    # extract the following information from l1, l2, l3

    nrows = # extract the number rows in the next section

    ncols = # extract the number of columns in the next section



    # loop while len(current_row) < nrows * ncols:


        # read next line, isolate the items using str.split()

        # append items to current_row



    # break current_row into the lines after each ncols-th item

    # store data in datasections in a new array


查看完整回答
反對(duì) 回復(fù) 2021-06-15
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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