大文本数据合并问题思路

发布于 2022-09-04 18:24:50 字数 2962 浏览 18 评论 0

背景:

我有三个csv文件,分别如下:

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

afile = 10G
bfile = 150G
cfile = 20G

注:各个field的分隔符并不是单个字符(例如逗号),而是一串特殊符号,因为部分field可能会包含某些单字符分隔符,键盘上的单字符都试过了,都有包含,所以用了一串几个字符组成的特殊字符串来分隔,所以并不是严格的csv,这是最蛋疼的地方

目的:

我想合并这三个文件,bfilecfile根据postid列合并,合并后再根据userid列合并afile,最终大概是postid, userid, postname, postnum, username这样的形式。

目前我的伪代码如下:

import pandas as pd
chunksize = 1000000  # 100W 目前看没问题
    try:
        resultchunktotal = []
        bfilereader = pd.read_csv(bfile,  iterator=True, engine='python', sep='##')
        goon_1 = True
        while goon_1:
            try:
                # 分块读取 bfile
                bfilechunk = 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有共同的postid
                                if 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='')
                                                # 不为空表示有共同的userid
                                                if not chunkresult.empty:
                                                    resultchunktotal.append(chunkresult)
                                        except StopIteration:
                                            goon_3 = False
                        except StopIteration:
                            goon_2 = False
            except StopIteration:
                goon_1 = False
        if len(resultchunktotal) > 0:
            pd.concat(resultchunktotal).to_csv('result.csv', index=False)
    except Exception as e:
        print(e)

但是感觉这样,很低效,所以跪求各位大神好的思路以及好的工具方法

ps: 这是一道“大数据”的伪命题,无非数据稍大了点

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2022-09-11 18:24:50

别写代码啦。看起来是一行 shell 脚本的事情,用 xsv join 子命令。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文