国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python - 大文本數據合并問題思路

瀏覽:106日期:2022-08-12 15:46:37

問題描述

背景:

我有三個csv文件,分別如下:

afile: userid, username, ....bfile: postid, userid, postname, ...cfile: postid, postnum, ...

afile = 10Gbfile = 150Gcfile = 20G

注:各個field的分隔符并不是單個字符(例如逗號),而是一串特殊符號,因為部分field可能會包含某些單字符分隔符,鍵盤上的單字符都試過了,都有包含,所以用了一串幾個字符組成的特殊字符串來分隔,所以并不是嚴格的csv,這是最蛋疼的地方

目的:

我想合并這三個文件,bfile和cfile根據postid列合并,合并后再根據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: 這是一道“大數據”的偽命題,無非數據稍大了點

問題解答

回答1:

別寫代碼啦。看起來是一行 shell 腳本的事情,用 xsv join 子命令。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 南雄市| 阜康市| 汝州市| 吴桥县| 伊吾县| 荆门市| 宁阳县| 天柱县| 鄢陵县| 合阳县| 贺兰县| 河间市| 翁牛特旗| 南澳县| 屯门区| 滦南县| 杭锦后旗| 万源市| 达拉特旗| 监利县| 菏泽市| 无极县| 孟村| 双鸭山市| 丹寨县| 金塔县| 萝北县| 花垣县| 眉山市| 会理县| 屏东市| 庄浪县| 浦县| 大新县| 平顺县| 洛南县| 曲水县| 建昌县| 枣阳市| 桃江县| 中江县|