Python 从json文件中删除新行空格并在dataframe中读取

Python 从json文件中删除新行空格并在dataframe中读取,python,json,pandas,newline,spaces,Python,Json,Pandas,Newline,Spaces,这是我的JSON文件的一个示例。我想把它加载到熊猫数据帧中 我尝试的方法是data=pd.read\u json(“test.json”,lines=True)。 但我在解码数组值(2)时发现了这个错误字符 我想我有了新行字符和额外的空格。如何删除并加载它们?您的示例json文件不正确,如果您使用的是lines=True,则每个对象都应该在新行中,而不是在单行中 {"country":"India","city":"Chennai","lat":13.0827,"lon":80.2707,"is

这是我的JSON文件的一个示例。我想把它加载到熊猫数据帧中

我尝试的方法是
data=pd.read\u json(“test.json”,lines=True)。

但我在解码数组值(2)时发现了这个错误字符
我想我有了新行字符和额外的空格。如何删除并加载它们?

您的示例json文件不正确,如果您使用的是lines=True,则每个对象都应该在新行中,而不是在单行中

{"country":"India","city":"Chennai","lat":13.0827,"lon":80.2707,"isp":"Facebook, Inc.","query":"157.240.23.19"}
{"country":"India","city":"Mumbai","lat":19.076,"lon":72.8777,"isp":"Facebook, Inc.","query":"31.13.79.18"}
{"country":"India","city":"Bhadohi","lat":25.3953,"lon":82.5703,"isp":"Railtel Enterprise Limited","query":"122.252.234.36"}
{"country":"India","city":"Mumbai","lat":19.076,"lon":72.8777,"isp":"Google LLC","query":"172.217.160.195"}
{"country":"India","city":"Gurugram","lat":28.4498,"lon":77.0761,"isp":"RailTel Corporation of India Ltd.","query":"122.252.245.12"}
{"country":"India","city":"Bhadohi","lat":25.3953,"lon":82.5703,"isp":"Railtel Enterprise Limited","query":"122.252.234.36"}
{"country":"India","city":"Chennai","lat":13.0827,"lon":80.2707,"isp":"Google LLC","query":"172.217.163.195"}

尝试使用lines=True,也尝试不使用,但仍然会得到相同的错误。我实际上引用了这个链接@KunalVerma,你需要在新行中有json对象,我编辑了我的答案来反映这一点。这些对象只在新行中。我认为有新行字符或空格,这就是我出错的原因
{"country":"India","city":"Chennai","lat":13.0827,"lon":80.2707,"isp":"Facebook, Inc.","query":"157.240.23.19"}
{"country":"India","city":"Mumbai","lat":19.076,"lon":72.8777,"isp":"Facebook, Inc.","query":"31.13.79.18"}
{"country":"India","city":"Bhadohi","lat":25.3953,"lon":82.5703,"isp":"Railtel Enterprise Limited","query":"122.252.234.36"}
{"country":"India","city":"Mumbai","lat":19.076,"lon":72.8777,"isp":"Google LLC","query":"172.217.160.195"}
{"country":"India","city":"Gurugram","lat":28.4498,"lon":77.0761,"isp":"RailTel Corporation of India Ltd.","query":"122.252.245.12"}
{"country":"India","city":"Bhadohi","lat":25.3953,"lon":82.5703,"isp":"Railtel Enterprise Limited","query":"122.252.234.36"}
{"country":"India","city":"Chennai","lat":13.0827,"lon":80.2707,"isp":"Google LLC","query":"172.217.163.195"}