读取 JSON 文件 Python 内有多个对象

Read JSON file with multiple objects inside Python

我正在尝试读取 Python 中的 json 文件并将其转换为数据帧。问题是我的 json 文件里面有几个 json 对象。我的json的结构是这样的:

{"Temp":"2,3", "OutsideTemp" : "3,4",...}
{"Temp":"3,2", "OutsideTemp" : "4,4",...}
{"Temp":"2,8", "OutsideTemp" : "3,7",...}
...

我试过使用 json 行和 pandas.read_json 但只出现错误。 (如您所见,我是 python 的菜鸟,帮帮我!)

您有一个 JSON Lines 格式的文本文件,因此设置 lines=True:

import pandas as pd

df = pd.read_json('data.txt', lines=True)
print(df)

输出

  Temp OutsideTemp
0  2,3         3,4
1  3,2         4,4
2  2,8         3,7

来自documentation

lines bool, default False
Read the file as a json object per line.

请注意,您必须将上面代码段中的 data.txt 更改为您的实际文件名。

import pandas as pd

df = pd.read_json('data.txt', lines=True)
df = df.apply(lambda x:x.str.replace(',','.')).astype(float)
print(df.info())
df
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 2 columns):
Temp           3 non-null float64
OutsideTemp    3 non-null float64
dtypes: float64(2)
memory usage: 176.0 bytes
None