我的文件包含许多字典,格式如下:
"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文件。我想读一下字典并计算它们。
答案 0 :(得分:3)
为什么要使用json,如果你没有json,只想知道你有多少个词?试试这个
with open( 'filename.json', 'r' ) as f :
data = f.read()
count = len( data.split('}{') )
答案 1 :(得分: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}]
答案 2 :(得分:1)
你必须定义编码
import json
with(open('filename.json', 'r')) as f:
data = f.read().decode("UTF-8")
print data.count('}')
答案 3 :(得分:1)
你也可以这样做:
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