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

2024-05-15 21:55:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用googlequickdraw数据集ndjson文件制作一个应用程序。我在文件的每一行运行此函数:

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

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

^{pr2}$

我得到以下错误:

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文件或代码进行哪些更改?我已经从googlegithub存储库中获取了代码。另外,我在数据集中使用简化的JSON文件。整个数据集是:

QuickDraw dataset


Tags: inpynonejsonlenreturnstrokenp
1条回答
网友
1楼 · 发布于 2024-05-15 21:55:40

可能是你的数据集有空行,你可以尝试添加这个来检查错误字符串

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

异常处理

^{pr2}$

相关问题 更多 >