【问题标题】:Processing a big file in python (>60gb)在 python 中处理一个大文件(>60gb)
【发布时间】:2020-09-21 14:30:40
【问题描述】:

我有一个文本文件(>= 60Gig),里面的记录是这样的:

{"index": {"_type": "_doc", "_id": "bLcy4m8BAObvGO9GALME"}}
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"flags\":2135,\"id\":816704468,\"access_hash\":\"788468819702098896\",\"first_name\":\"a\",\"last_name\":\"b\",\"phone\":\"123\",\"status\":{\"_\":\"userStatusOffline\",\"was_online\":132}}","phone":"12","@version":"1","typ":"telegram_contacts","access_hash":"123","id":816704468,"@timestamp":"2020-01-26T13:53:29.467Z","path":"/home/user/mirror_01/users_5d6ca02e7e736a7fc700df8c.log","type":"redis","flags":2135,"host":"ubuntu","imported_from":"telegram_contacts"}

{"index": {"_type": "_doc", "_id": "Z7cy4m8BAObvGO9GALME"}}
{"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"flags\":2143,\"id\":323586643,\"access_hash\":\"8315858910992970114\",\"first_name\":\"bv\",\"last_name\":\"nj\",\"username\":\"kj\",\"phone\":\"123\",\"status\":{\"_\":\"userStatusRecently\"}}","phone":"123","@version":"1","typ":"telegram_contacts","access_hash":"8315858910992970114","id":323586643,"@timestamp":"2020-01-26T13:53:29.469Z","path":"/home/user/mirror_01/users_5d6ca02e7e736a7fc700df8c.log","username":"mbnab","type":"redis","flags":2143,"host":"ubuntu","imported_from":"telegram_contacts"}

对此我有几个问题:

  1. 这是一个有效的 JSON 文件吗?
  2. python 可以处理这种大小的文件吗?还是应该以某种方式将其转换为 Access 或 Excel 文件?

这些是我发现有用的一些 SO 帖子:

但仍需要帮助。

【问题讨论】:

  • 我不认为那是 json。
  • @Yatin 我也是。你认为我应该为此做一个模板吗?
  • "message" 的值是否只是保存为字符串并带有"转义"的json?
  • @MichaelC 是的,先生。这是准确的记录。
  • 使用 python 可以处理这个大小的文件....但是你想用它做什么呢?你试过什么?

标签: python text-processing python-textprocessing


【解决方案1】:

您可以逐行处理文件并提取您需要的信息。

with open('largefile.txt','r') as f:
    for line in f:
        # Extract what you need from that line of text here
        print(line)

例如,要读取内容,您可以逐行处理文件并提取所需的信息。

with open('largefile.txt','r') as f:
    for line in f:
        # For example, to interpret the string as json, and read 
        # it in as a dictionary, do 
        if line.strip():  # check there is something on the line
            data = json.loads(line)
            # in your case, to fix the value for "message" do
            if 'message' in data: 
                data['message'] = json.loads(data['message']) 
            # extract information you need here

我预计还有很多工作可以提取您需要的信息,但我希望这可以帮助您入门。祝你好运!

【讨论】:

  • 谢谢!如果它解决了您的问题,请接受它是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 2011-08-07
  • 2017-03-22
相关资源
最近更新 更多