【问题标题】:Read multiple dictionaries containing integers from json file in Python从 Python 中的 json 文件中读取包含整数的多个字典
【发布时间】:2016-12-13 14:29:57
【问题描述】:

我有一个包含许多字典的文件,格式如下:

"string":integer

例如:

{"1":1, "2":4, "3":24}{"1":1, "2":6, "3":50}

此文件保存为 .json。由于它不是正确的 json 格式,当我尝试读取文件时:

with(open('filename.json', 'r') as f:
    data = json.load(f)

我明白了

ValueError: Extra data: line 1 column 101 - line 1 column 748538 (char 100 - 748537)

我看到了this answer,但它似乎只与字符串有关。如何加载我的文件,然后计算它包含的字典数量?

编辑:

作为对评论的回应,以下是文件的更多内容:

{"1": 1, "2": 4, "3": 24, "4": 57, "5": 184, "6": 166, "7": 115, "8": 33, "9": 13, "10": 3, "11": 1}{"1": 1, "2": 2, "3": 8, "4": 47, "5": 129, "6": 208, "7": 127, "8": 48, "9": 28, "10": 7, "11": 1}{"1": 1, "2": 2, "3": 11, "4": 56, "5": 146, "6": 204, "7": 139, "8": 33, "9": 9, "10": 2, "11": 3}{"1": 1, "2": 1, "3": 0, "4": 6, "5": 16, "6": 69, "7": 196, "8": 153, "9": 107, "10": 36, "11": 16, "12": 3}{"1": 1, "2": 5, "3": 40, "4": 128, "5": 200, "6": 151, "7": 59, "8": 10, "9": 3}{"1": 1, "2": 5, "3": 25, "4": 77, "5": 178, "6": 147, "7": 64, "8": 52, "9": 27, "10": 10, "11": 4}{"1": 1, "2": 1, "3": 12, "4": 37, "5": 132, "6": 210, "7": 144, "8": 50, "9": 11, "10": 5}{"1": 1, "2": 5, "3": 21, "4": 52, "5": 137, "6": 223, "7": 121, "8": 35, "9": 3, "10": 1, "11": 2}{"1": 1, "2": 3, "3": 11, "4": 35, "5": 71, "6": 168, "7": 154, "8": 85, "9": 46, "10": 20, "11": 8, "12": 6, "13": 1}{"1": 1, "2": 10, "3": 43, "4": 120, "5": 217, "6": 151, "7": 45, "8": 8, "9": 4, "10": 1}{"1": 1, "2": 3, "3": 22, "4": 78, "5": 223, "6": 182, "7": 67, "8": 19, "9": 2}{"1": 1, "2": 0, "3": 3, "4": 3, "5": 10, "6": 35, "7": 124, "8": 210, "9": 150, "10": 46, "11": 10, "12": 2, "13": 1}{"1": 1, "2": 4, "3": 22, "4": 69, "5": 206, "6": 206, "7": 69, "8": 15, "9": 4, "10": 1}

是的,它不是有效的 json。但它保存为 .json 文件。我想读字典,数一数。

【问题讨论】:

  • 我认为那不是有效的 json。
  • 可以尝试全部阅读并在}{ 上执行.split() 或其他操作
  • 您能否包含文件的前 5 行,这样人们就不必猜测其中可能包含什么内容?
  • 你只想数字典吗?
  • 如果您的 json 文件是另一个自制程序的输出,则更改如下 [{"1":1, "2":4, "3":24},{"1": 1,“2”:6,“3”:50}]。这是有效的 json 并且 json.load() 不会失败。

标签: python json dictionary


【解决方案1】:

如果你没有 json 并且只想知道你有多少个字典,为什么要使用 json?试试这个

with open( 'filename.json', 'r' ) as f :
    data = f.read()

count = len( data.split('}{') )

【讨论】:

    【解决方案2】:

    假设您的数据不包含带有 }{ 的 JSON 字符串,您可以将它们转换为数组,然后进行解析:

    >>> import json
    >>> s = '{"1":1, "2":4, "3":24}{"1":1, "2":6, "3":50}'
    >>> res = json.loads('[' + s.replace('}{', '},{') + ']')
    >>> res
    [{u'1': 1, u'3': 24, u'2': 4}, {u'1': 1, u'3': 50, u'2': 6}]
    

    【讨论】:

    • 该文件包含字典,而不是字符串,所以我不知道如何使这种类型的加载工作。我想我需要先有字符串,然后加载 json?
    • @StatsSorceress 这些行将被读取为字符串。一旦你将它们重新格式化为正确的 json 格式,json.loads 将成功解析这些行。
    【解决方案3】:

    你必须定义编码

    import json
    with(open('filename.json', 'r')) as f:
        data = f.read().decode("UTF-8")
    print data.count('}')
    

    【讨论】:

    • 这是一个很好的开始,因为它读取文件,但将其解析为字符串,然后我无法计算字典的数量。
    【解决方案4】:

    你也可以这样做:

    with open('filename', 'r') as f:
        a = f.read()
    print a.count('}{')+1
    

    有时 Bash 工具非常强大。请考虑使用它们。你也可以这样算:

    import subprocess
    
    print int(subprocess.check_output("grep -o '}{' /home/yusuf/Desktop/c21 | wc -l", shell=True))+1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-03
      • 2019-01-31
      • 2015-02-23
      • 2018-09-22
      • 2012-07-07
      • 2018-06-16
      • 1970-01-01
      相关资源
      最近更新 更多