报错背景
在huggingface上传数据集后,Dataset Viewer无法显示,报错:
The dataset viewer is not available for this split.
Cannot extract the features (columns) for the split 'train' of the config 'default' of the dataset.
Error code: FeaturesError
Exception: ValueError
Message: Not able to read records in the JSON file at hf://datasets/xxx/train.json.
Traceback: Traceback (most recent call last):
File "/src/services/worker/src/worker/job_runners/split/first_rows.py", line 243, in compute_first_rows_from_streaming_response
iterable_dataset = iterable_dataset._resolve_features()
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 2215, in _resolve_features
features = _infer_features_from_batch(self.with_format(None)._head())
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 1239, in _head
return _examples_to_batch(list(self.take(n)))
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 1388, in __iter__
for key, example in ex_iterable:
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 1044, in __iter__
yield from islice(self.ex_iterable, self.n)
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 282, in __iter__
for key, pa_table in self.generate_tables_fn(**self.kwargs):
File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/packaged_modules/json/json.py", line 164, in _generate_tables
raise ValueError(f"Not able to read records in the JSON file at {file}.") from None
ValueError: Not able to read records in the JSON file at hf://datasets/xxx/train.json.
查看了别人展示的数据展示都好好的,就下载了一个看一下
发现别人的json文件都是一行一个字典
而我的是一个列表包含了很多字典
所以我就开始想办法转换成他们那样
import json
# 读取 JSON 文件
with open('train.json', 'r', encoding='utf-8') as f:
data = json.load(f)
# 将每个字典写入到新文件中,每行一个字典
with open('train_new.json', 'w', encoding='utf-8') as f:
for item in data:
json_string = json.dumps(item, ensure_ascii=False)
f.write(json_string + '\n')
print("处理完成")
然后我就在VSCode打开我的新文件,就出现了报错预期为文件结尾。json [行2,列1]
报错原因
很低级的错误,我问了chatgtp4和claude都没有回答上来,百度搜索也搜不到
其实就是保存文件时应该是jsonl
解决方案
把json
换成jsonl
# # 将每个字典写入到新文件中,每行一个字典
with open('train_new.jsonl', 'w', encoding='utf-8') as f:
for item in data:
json_string = json.dumps(item, ensure_ascii=False)
f.write(json_string + '\n')
print("处理完成")
结果展示
最后dataset viewer 正常展示了