报错背景
在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_responseiterable_dataset = iterable_dataset._resolve_features()File "/src/services/worker/.venv/lib/python3.9/site-packages/datasets/iterable_dataset.py", line 2215, in _resolve_featuresfeatures = _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 _headreturn _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_tablesraise ValueError(f"Not able to read records in the JSON file at {file}.") from NoneValueError: 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
最后dataset viewer 正常展示了