python - pandas 无法将以下 txt 文件解析为 pandas 数据框,只有一列
问题描述
我有这样的txt
文件格式:
[{
"lng": "111.68389897298637",
"odometer": 0,
"busSpeed": "0.00",
"relativeLocation": 0,
"realTimeStatus": "0",
"driverUuid": "28b3ea130a6ad049a10b041037e2550d80f5",
"posUuid": "8eba1ae2fd014eaf948b",
"cursorOverGround": "0",
"posIsInStation": "",
"sationName": "",
"distanceToPrePosition": 0,
"lineUuid": "c2e0313000fba04c940a4be0f9edb9c521c8",
"drvIcCard": "3168310624",
"lineType": "0",
"isOffset": "1",
"driverName": "贾爱俊",
"gatherTime": 1506468898000,
"allAlarms": "7",
"busUuid": "9d9648ff0375a04702080cc03a4c20377ea8",
"lat": "40.86856137377131",
"devUuid": "3d16fedb858842199cb3",
"sationUuid": ""
}, {
"lng": "111.68364330061954",
"odometer": 0,
"busSpeed": "11.00",
"relativeLocation": 0,
"realTimeStatus": "0",
"driverUuid": "28b3ea130a6ad049a10b041037e2550d80f5",
"posUuid": "ff42c6cca2e14583b84f",
"cursorOverGround": "280",
"posIsInStation": "",
"sationName": "",
"distanceToPrePosition": 0,
"lineUuid": "c2e0313000fba04c940a4be0f9edb9c521c8",
"drvIcCard": "3168310624",
"lineType": "0",
"isOffset": "1",
"driverName": "贾爱俊",
"gatherTime": 1506468919000,
"allAlarms": "7",
"busUuid": "9d9648ff0375a04702080cc03a4c20377ea8",
"lat": "40.868510932496115",
"devUuid": "3d16fedb858842199cb3",
"sationUuid": ""
}]
当我想将它导入数据框时,我只得到一列:
但是当我选择一些列表时,txt
我可以得到正确的答案:
谁能告诉我如何正确处理该txt
文件?任何帮助表示赞赏!谢谢。
解决方案
你需要pandas.read_json
. 下面的完整示例。
设置
import pandas as pd
from io import StringIO
mystr = StringIO("""[{
"lng": "111.68389897298637",
"odometer": 0,
"busSpeed": "0.00",
"relativeLocation": 0,
"realTimeStatus": "0",
"driverUuid": "28b3ea130a6ad049a10b041037e2550d80f5",
"posUuid": "8eba1ae2fd014eaf948b",
"cursorOverGround": "0",
"posIsInStation": "",
"sationName": "",
"distanceToPrePosition": 0,
"lineUuid": "c2e0313000fba04c940a4be0f9edb9c521c8",
"drvIcCard": "3168310624",
"lineType": "0",
"isOffset": "1",
"driverName": "贾爱俊",
"gatherTime": 1506468898000,
"allAlarms": "7",
"busUuid": "9d9648ff0375a04702080cc03a4c20377ea8",
"lat": "40.86856137377131",
"devUuid": "3d16fedb858842199cb3",
"sationUuid": ""
}, {
"lng": "111.68364330061954",
"odometer": 0,
"busSpeed": "11.00",
"relativeLocation": 0,
"realTimeStatus": "0",
"driverUuid": "28b3ea130a6ad049a10b041037e2550d80f5",
"posUuid": "ff42c6cca2e14583b84f",
"cursorOverGround": "280",
"posIsInStation": "",
"sationName": "",
"distanceToPrePosition": 0,
"lineUuid": "c2e0313000fba04c940a4be0f9edb9c521c8",
"drvIcCard": "3168310624",
"lineType": "0",
"isOffset": "1",
"driverName": "贾爱俊",
"gatherTime": 1506468919000,
"allAlarms": "7",
"busUuid": "9d9648ff0375a04702080cc03a4c20377ea8",
"lat": "40.868510932496115",
"devUuid": "3d16fedb858842199cb3",
"sationUuid": ""
}]""")
解决方案
# replace mystr with 'file.txt'
df = pd.read_json(mystr)
结果
print(df)
allAlarms busSpeed busUuid cursorOverGround \
0 7 0.00 9d9648ff0375a04702080cc03a4c20377ea8 0
1 7 11.00 9d9648ff0375a04702080cc03a4c20377ea8 280
devUuid distanceToPrePosition driverName \
0 3d16fedb858842199cb3 0 贾爱俊
1 3d16fedb858842199cb3 0 贾爱俊
driverUuid drvIcCard gatherTime \
0 28b3ea130a6ad049a10b041037e2550d80f5 3168310624 1506468898000
1 28b3ea130a6ad049a10b041037e2550d80f5 3168310624 1506468919000
... lineType lineUuid \
0 ... 0 c2e0313000fba04c940a4be0f9edb9c521c8
1 ... 0 c2e0313000fba04c940a4be0f9edb9c521c8
lng odometer posIsInStation posUuid \
0 111.68389897298637 0 8eba1ae2fd014eaf948b
1 111.68364330061954 0 ff42c6cca2e14583b84f
realTimeStatus relativeLocation sationName sationUuid
0 0 0
1 0 0
推荐阅读
- c# - 你如何制作一个倒数计时器(包括毫秒分钟和秒)c#
- python - 将列表附加到列表列表
- reactjs - 如何在任何.scss 文件中使用tailwind 的@apply 指令,而不是仅使用它的主tailwind 文件(在React 中)?
- angular - HTML 中的 Angular 2+ 组件文档预览
- apache-spark - 使用pyspark同时进行聚合和特征提取
- firebase - 安装:无法处理请求。离线申请。firebase 的(安装/应用离线)错误
- android - ImageView 不会替换之前在 RecyclerView Adapter 中使用的 Drawable
- flutter - 无法使用 Navigator.push() 使用电子邮件和密码登录到 Flutter 中的其他窗口
- audio - 在 Arduino 上播放多个连续音调?
- javascript - 使用 onClick 更新 Javascript 变量