首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json.decoder.JSONDecodeError:期望值:从Google QuickDraw数据集读取json文件的第1列(char 0)

json.decoder.JSONDecodeError:期望值:从Google QuickDraw数据集读取json文件的第1列(char 0)
EN

Stack Overflow用户
提问于 2019-02-24 03:43:27
回答 1查看 1K关注 0票数 0

我正在制作一个应用程序使用谷歌QuickDraw数据集ndjson文件。我在文件的每一行上运行这个函数:

代码语言:javascript
复制
def parse_line(ndjson_line):
    """Parse an ndjson line and return ink (as np array) and classname."""
    sample = json.loads(ndjson_line)
    class_name = sample["word"]
    if not class_name:
        print("Empty classname")
        return None, None
    inkarray = sample["drawing"]
    stroke_lengths = [len(stroke[0]) for stroke in inkarray]
    total_points = sum(stroke_lengths)
    np_ink = np.zeros((total_points, 3), dtype=np.float32)
    current_t = 0
    if not inkarray:
        print("Empty inkarray")
        return None, None
    for stroke in inkarray:
        if len(stroke[0]) != len(stroke[1]):
            print("Inconsistent number of x and y coordinates.")
            return None, None
    for i in [0, 1]:
        np_ink[current_t:(current_t + len(stroke[0])), i] = stroke[i]
    current_t += len(stroke[0])
    np_ink[current_t - 1, 2] = 1  # stroke_end
    # Preprocessing.
    # 1. Size normalization.
    lower = np.min(np_ink[:, 0:2], axis=0)
    upper = np.max(np_ink[:, 0:2], axis=0)
    scale = upper - lower
    scale[scale == 0] = 1
    np_ink[:, 0:2] = (np_ink[:, 0:2] - lower) / scale
    # 2. Compute deltas.
    np_ink[1:, 0:2] -= np_ink[0:-1, 0:2]
    np_ink = np_ink[1:, :]
    return np_ink, class_name

它适用于大多数线路,但适用于少数几行,例如:

代码语言:javascript
复制
{"word":"wristwatch","countrycode":"FR","timestamp":"2017-01-19 09:30:18.19194 UTC","recognized":true,"key_id":"6721203257475072","drawing":[[[0,143],[66,67]],[[1,170],[35,39]],[[169,169,179,186,187,193,216,228,228,225,249,254,255,249,244,246,251,254,242,226,232,238,237,224,235,234,211,201,197,192,170,160,144,141,142],[39,26,7,9,25,15,2,2,12,19,7,23,36,39,34,32,37,56,54,44,47,58,67,65,74,80,84,82,75,92,73,97,85,71,67]],[[94,96,110],[26,88,89]]]}

我得到了以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 168, in <module>
    tf.app.run(main=main,argv=[sys.argv[0]]+unparsed)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\platform\app.py", line 125, in run
    _sys.exit(main(argv))
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 124, in main
    classes = convert_to_tfrecord(FLAGS.source_path,FLAGS.destination_path,FLAGS.train_examples_per_class,FLAGS.eval_examples_per_class,FLAGS.classes_path,FLAGS.output_shards)
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 98, in convert_to_tfrecord
    drawing, class_name  = parse_line(ndjson_line)
  File "A:\Code\Machine Learning\Software Engineering project\Quick Draw\Create_Dataset_from_ndjson.py", line 13, in parse_line
    sample = json.loads(ndjson_line)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\shind\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

造成这一错误的原因是什么?它写的是期望值,但我已经过了线,那么它需要什么呢?而且,这看起来与我所传递的其他行没有什么不同,这些行不会产生任何错误,那么这行又是什么呢?我需要对JSON文件或代码进行哪些更改?我已经从Google GitHub存储库本身获取了代码。另外,我在数据集中使用简化的JSON文件的数据集。整个数据集是:

QuickDraw数据集

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-24 04:29:56

也许您的数据集有空行,您可以尝试添加这个以检查错误字符串。

代码语言:javascript
复制
try:
    sample = json.loads(ndjson_line)
except json.decoder.JSONDecodeError as e:
    print("Error Line: {}\n".format(ndjson_line)) # Print the Decode Error string
    raise e # To Stop the Program

更新#2

异常处理

代码语言:javascript
复制
try:
    sample = json.loads(ndjson_line)
except json.decoder.JSONDecodeError as e:
    return None, None
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54848521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档