pandas无法将以下txt文件解析为pandas数据帧,只需要一列

时间:2018-05-15 07:45:50

标签: python pandas csv dataframe

我有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": ""
}]

当我想将其导入数据框时,我只得到一列:

column

但是当我选择txt的某些列表时,我可以得到正确答案:

enter image description here

有谁可以告诉我如何使用txt文件正确执行此操作?任何帮助赞赏!感谢。

2 个答案:

答案 0 :(得分:0)

无法撰写评论,因此必须写一个答案。

首先查看以下链接:

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_table.html https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

您很可能必须检查分隔符/ sep和标头参数,以便正确地将文件解析为pandas DataFrame。

请提供最少的代码示例,说明您目前为止所尝试的内容或代码导致您的问题。

只需将上面的示例与python中的列表一起使用就可以了:

import pandas as pd  
file = [{
...   "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": ""  
... }]  


df = pd.DataFrame(file)
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

[2 rows x 22 columns]

答案 1 :(得分:0)

您需要pandas.read_json。完整的例子如下。

<强>设置

interceptUrlMap: [
            {pattern: '/',               access: ['permitAll']},
            {pattern: '/error',          access: ['permitAll']},
            {pattern: '/index',          access: ['permitAll']},
            {pattern: '/index.gsp',      access: ['permitAll']},
            {pattern: '/shutdown',       access: ['permitAll']},
            {pattern: '/assets/**',      access: ['permitAll']},
            {pattern: '/**/js/**',       access: ['permitAll']},
            {pattern: '/**/css/**',      access: ['permitAll']},
            {pattern: '/**/images/**',   access: ['permitAll']},
            {pattern: '/**/favicon.ico', access: ['permitAll']},
            {pattern: '/login/**',       access: ['permitAll']},
            {pattern: '/logout/**',      access: ['permitAll']}
        ]

<强>解决方案

jQuery.sap.require("sap.ndc.BarcodeScanner");
sap.ndc.BarcodeScanner.scan(
    function (oResult) { / * process scan result * / },
    function (oError) { / * handle scan error * / },
    function (oResult) { / * handle input dialog change * / }
);

结果

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": ""
}]""")