使用 pathlib 将 json 目录复制到 df

directory of jsons to df with pathlib

提问人:M. Wood 提问时间:11/16/2023 更新时间:11/17/2023 访问量:41

问:

我在目录中有一组 json,我试图循环访问并读取到 Pandas dfpathlib.Path('my_jsons_dir').iterdir()

单个文件的情况效果很好

json_path = pathlib.Path('my_json_path')
dict_single = json.loads(json_path.read_bytes())
df_single = pd.DataFrame.from_dict(pd.json_normalize(dict_single), orient='columns')

但是循环打嗝......

file_paths = pathlib.Path(file_dir)
data = []

for file in file_paths.iterdir():
    if file.is_file():
        dict = json.loads(file.read_bytes())
        df = pd.DataFrame.from_dict(pd.json_normalize(dict), orient='columns')
        data.append(df)

错误信息

JSONDecodeError                           Traceback (most recent call last)
Cell In[34], line 8
      6 for file in file_paths.iterdir():
      7     if file.is_file():
----> 8         dict = json.loads(file.read_bytes())
      9         df = pd.DataFrame.from_dict(pd.json_normalize(dict), orient='columns')
     10         data.append(df)

File c:\Users\mdw0523\AppData\Local\Programs\Python\Python310\lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    341     s = s.decode(detect_encoding(s), 'surrogatepass')
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:
    348     cls = JSONDecoder

File c:\Users\mdw0523\AppData\Local\Programs\Python\Python310\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    332 def decode(self, s, _w=WHITESPACE.match):
    333     """Return the Python representation of ``s`` (a ``str`` instance
    334     containing a JSON document).
    335 
    336     """
--> 337     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
...
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

它看起来像一个简单的打字,但不明白为什么它的工作方式与单文件情况下的工作方式不同,因为需要跨 mac/win 灵活工作dict = json.loads(file.read_bytes())libpathos

感谢您的帮助!

python json 熊猫

评论


答:

0赞 M. Wood 11/17/2023 #1

事实证明,错误是由不包含文本的文件之一引起的。这个问题可以关闭。