python - 大文本數(shù)據(jù)合并問題思路
問題描述
背景:
我有三個(gè)csv文件,分別如下:
afile: userid, username, ....bfile: postid, userid, postname, ...cfile: postid, postnum, ...
afile = 10Gbfile = 150Gcfile = 20G
注:各個(gè)field的分隔符并不是單個(gè)字符(例如逗號(hào)),而是一串特殊符號(hào),因?yàn)椴糠謋ield可能會(huì)包含某些單字符分隔符,鍵盤上的單字符都試過了,都有包含,所以用了一串幾個(gè)字符組成的特殊字符串來分隔,所以并不是嚴(yán)格的csv,這是最蛋疼的地方
目的:
我想合并這三個(gè)文件,bfile和cfile根據(jù)postid列合并,合并后再根據(jù)userid列合并afile,最終大概是postid, userid, postname, postnum, username這樣的形式。
目前我的偽代碼如下:
import pandas as pdchunksize = 1000000 # 100W 目前看沒問題 try:resultchunktotal = []bfilereader = pd.read_csv(bfile, iterator=True, engine=’python’, sep=’##’)goon_1 = Truewhile goon_1: try:# 分塊讀取 bfilebfilechunk = bfilereader.get_chunk(chunksize)if not bfilechunk.empty: cfilereader = pd.read_csv(cfile, iterator=True, engine=’python’, sep=’##’) goon_2 = True while goon_2:try: # 分塊讀取 cfile cfilechunk = cfilereader.get_chunk(chunksize) if not cfilechunk.empty:bfilecfilechunk = pd.merge(bfilechunk, cfilechunk, on=’postid’)# 不為空代表 bfile cfile有共同的postidif not bfilecfilechunk.empty: afilereader = pd.read_csv(afile, iterator=True, engine=’python’, sep=’##’) goon_3 = True while goon_3:try: # 分塊讀取afile afilechunk = afilereader.get_chunk(chunksize) if not afilechunk.empty:chunkresult = pd.merge(bfilecfilechunk, afilechunk, on=’’)# 不為空表示有共同的useridif not chunkresult.empty:resultchunktotal.append(chunkresult)except StopIteration: goon_3 = Falseexcept StopIteration: goon_2 = False except StopIteration:goon_1 = Falseif len(resultchunktotal) > 0: pd.concat(resultchunktotal).to_csv(’result.csv’, index=False) except Exception as e:print(e)
但是感覺這樣,很低效,所以跪求各位大神好的思路以及好的工具方法
ps: 這是一道“大數(shù)據(jù)”的偽命題,無非數(shù)據(jù)稍大了點(diǎn)
問題解答
回答1:別寫代碼啦。看起來是一行 shell 腳本的事情,用 xsv join 子命令。
相關(guān)文章:
1. mysql建表報(bào)錯(cuò),查手冊(cè)看不懂,求解?2. 求大神支招,php怎么操作在一個(gè)html文件的<head>標(biāo)記內(nèi)添加內(nèi)容?3. 致命錯(cuò)誤: Class ’appfacadeTest’ not found4. PHP類屬性聲明?5. php點(diǎn)贊一天一次怎么實(shí)現(xiàn)6. 怎么php怎么通過數(shù)組顯示sql查詢結(jié)果呢,查詢結(jié)果有多條,如圖。7. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)8. 老師們php,插入數(shù)據(jù)庫(kù)mysql,都是空的,要怎么解決9. sql語句 - 如何在mysql中批量添加用戶?10. phpstady在win10上運(yùn)行
