python - 大文本数据合并问题思路
问题描述
背景:
我有三个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 子命令。
相关文章:
1. javascript - 百度坐标,返回目标附近有哪些坐标2. javascript - 关于jsp中嵌入js代码的问题3. javascript - 判断用户网络环境4. javascript - 对不同类别的id的物品中的某个字段做权重覆盖,除了直接使用if else语句还能有更优雅便于维护的方式吗?5. javascript - 一个JS多元运算符的问题6. javascript - 关于fullpage.js的横向滚动控制7. javascript - 为什么var obj = {}创建对象的方法里面不能用this.xxx来声明属性 ?8. javascript - 有什么兼容性比较好的办法来判断浏览器窗口的类型?9. javascript - 有一个异步获取数据的函数A,其他依赖这个A得到的数据的函数是否都必须是异步的?10. angular.js - angurlarJS的安卓的微信端中用加载ngCookies会出错的问题

网公网安备