【发布时间】:2018-04-19 16:46:49
【问题描述】:
例如,假设我有如下代码:
def function_callback(cb):
cb()
def rand_name_giving_func(i):
list_test = ['john', 'jim', 'anna', 'cynthia', 'dwight']
return list_test[i] # not that random
def rand_value(i):
dict_test = {'0': 'random_string', '1': 'random_string', '2': 'random_string'}
return dict_test[str(i)]
def example():
data = {}
for i in range(3):
data['name_' + str(i)] = rand_name_giving_func(i)
data['value_' + str(i)] = rand_value(i)
if os.path.isfile('file.json') == True:
with open('file.json', 'r') as fp:
temp = json.load(fp)
temp.update(data)
with open('file.json', 'w') as fp:
json.dump(temp, fp, indent=4, sort_keys=True)
else:
with open('file.json', 'w') as fp:
json.dump(data, fp, indent=4, sort_keys=True)
if __name__ == '__main__':
for i in range(10000):
function_callback(example)
假设我只能处理 example() 内的 JSON 文件,并且回调会发生多次。据我了解,同一文件不会发生多个json.dump() 调用,因此我发现如果我反序列化文件,更新生成的字典并再次序列化(尽管效率极低),它可以工作。它没有,所以我得到了如下错误:
Traceback (most recent call last):
File "/home/pxcel/example.py", line 90, in function_callback
temp = json.load(fp)
File "/usr/lib/python2.7/json/__init__.py", line 291, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: end is out of bounds
还有ValueError: No JSON object could be decoded,
ValueError: Extra data: line,所有关于temp = json.load(fp)。
我已经搜索过处理 JSON(ijson、demjson 等)的替代模块,但是 我还没有找到一种有用的方法来使用它们来解决上述问题。 假设 JSON 文件结构如下所示:
{
"name_0": "john",
"value_0": {
"0": "random_string"
}
}
有什么想法吗?假设 list_test 和 dict_test 各有 100k 个值,并且回调发生 10k 次。 JSON 编码/解码会起作用吗?
【问题讨论】:
-
这是您代码中的错字吗?
temp. = json.load(fp)。当您在with语句中进行open()调用时,您是否尝试过printingtemp的内容? -
请将您的程序缩减为能够显示错误的最短的完整程序。请edit您的问题并将简短的完整程序复制粘贴到您的问题中。您的读者应该能够将其从Stack Overflow 复制粘贴到文本文件中并运行它。请参阅minimal reproducible example 了解更多信息。
-
@user8212173 是的,这是一个错字。不,我没有,是否有可能转换为字典有问题?
-
它通常不是,但是通过打印每一行并使用JsonLint 验证 json 来确保 json 数据可以被 python 读取。有时,您的 json 数据中可能存在可能导致编码/解码错误的空格。您问题中的 JSON 示例是有效的,我无法重现此错误。
-
另外,还不清楚
rand_name_giving_func()和rand_value()的作用。当这些值被更新到临时字典时,很可能会出现错误。