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

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

在循環(huán)理解中使用 open() - 獲取目錄中所有文件的文本內(nèi)容列表

在循環(huán)理解中使用 open() - 獲取目錄中所有文件的文本內(nèi)容列表

森林海 2021-10-12 17:35:05
有沒有更好的方法來使用with open(file) as f: f.read()for 循環(huán)內(nèi)的機制 - 即對許多文件進行操作的循環(huán)理解?我試圖將它放入一個數(shù)據(jù)幀中,以便存在從文件到文件內(nèi)容的映射。這是我所擁有的 - 但它似乎效率低下并且不是pythonic/可讀的:documents = pd.DataFrame(glob.glob('*.txt'), columns = ['files'])documents['text'] = [np.nan]*len(documents)for txtfile in documents['files'].tolist():    if txtfile.startswith('GSE'):        with open(txtfile) as f:            documents['text'][documents['files']==txtfile] = f.read()輸出:    files   text0   GSE2640_GSM50721.txt    | RNA was extracted from lung tissue using a T...1   GSE7002_GSM159771.txt   Array Type : Rat230_2 ; Amount to Core : 15 ; ...2   GSE1560_GSM26799.txt    | C3H denotes C3H / HeJ mice whereas C57 denot...3   GSE2171_GSM39147.txt    | HIV seropositive , samples used to test HIV ...
查看完整描述

2 回答

?
阿晨1998

TA貢獻2037條經(jīng)驗 獲得超6個贊

你可以做:


# import libraries

import os,pandas


# list filenames, assuming your path is './'

files = [i for i in os.listdir('./') if i[:3]=='GSE' and i[-3:]=='txt']


# get contents of files

contents = []

for i in files:

    with open(i) as f: contents.append(f.read().strip())


# into a nice table 

table = pandas.DataFrame(contents, index=files, columns=['text'])


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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