如何拆分并将输出文件转换为带列名的列?

huangapple go评论135阅读模式
英文:

How can I split and convert the output file to columns with column names?

问题

有一个文件如下。

> {时间: "2014-01-01 00:22:01.150000000", 缩放后的加速度X: "0.039298", 缩放后的加速度Y: "0.005923", 缩放后的加速度Z: "-0.994687", 缩放后的陀螺仪X: "0.001538", 缩放后的陀螺仪Y: "0.000883", 缩放后的陀螺仪Z: "-0.000879", 缩放后的磁力计X: "0.325602", 缩放后的磁力计Y: "-0.209304", 缩放后的磁力计Z: "0.145305", 缩放后的环境压力: "1016.627380", deltaThetaX: "0.000141", deltaThetaY: "0.000108", deltaThetaZ: "-0.000096", deltaVelX: "0.003646", deltaVelY: "0.000563", deltaVelZ: "-0.093733", 横滚角: "-0.005220", 俯仰角: "0.042930", 偏航角: "0.558326", 方向四元数: "[0.961046,-0.00842277,0.0199133,0.27554]", 方向矩阵: "[[0.847362,0.529279,-0.0429168],[-0.52995,0.848013,-0.0052155],[0.0336336,0.0271632,0.999065]]", 稳定后的磁力计X: "0.325882", 稳定后的磁力计Y: "-0.208165", 稳定后的磁力计Z: "0.148896", 稳定后的加速度X: "0.042812", 稳定后的加速度Y: "0.005203", 稳定后的加速度Z: "-0.996619", GPS相关时间戳Tow: "309057.015000", GPS相关时间戳周数: "2245", GPS相关时间戳标志: "5"},
{时间: "2014-01-01 00:22:01.200000000", 缩放后的加速度X: "0.038701", 缩放后的加速度Y: "0.007346", 缩放后的加速度Z: "-0.997266", 缩放后的陀螺仪X: "0.001427", 缩放后的陀螺仪Y: "0.001284", 缩放后的陀螺仪Z: "-0.001059", 缩放后的磁力计X: "0.326149", 缩放后的磁力计Y: "-0.211073", 缩放后的磁力计Z: "0.147229", 缩放后的环境压力: "1016.640015", deltaThetaX: "0.000049", deltaThetaY: "0.000060", deltaThetaZ: "-0.000060", deltaVelX: "0.001957", deltaVelY: "0.000418", deltaVelZ: "-0.049938", 横滚角: "-0.005225", 俯仰角: "0.042927", 偏航角: "0.558373", 方向四元数: "[0.96104,-0.00842497,0.0199111,0.275563]", 方向矩阵: "[[0.847337,0.529319,-0.042914],[-0.52999,0.847988,-0.00521991],[0.0336276,0.027167,0.999065]]", 稳定后的磁力计X: "0.325892", 稳定后的磁力计Y: "-0.208194", 稳定后的磁力计Z: "0.148905", 稳定后的加速度X: "0.042810", 稳定后的加速度Y: "0.005207", 稳定后的加速度Z: "-0.996652", GPS相关时间戳Tow: "309057.065000", GPS相关时间戳周数: "2245", GPS相关时间戳标志: "5"},

英文:

I have a file like below.

> {time: "2014-01-01 00:22:01.150000000", scaledAccelX: "0.039298", scaledAccelY: "0.005923", scaledAccelZ: "-0.994687", scaledGyroX: "0.001538", scaledGyroY: "0.000883", scaledGyroZ: "-0.000879", scaledMagX: "0.325602", scaledMagY: "-0.209304", scaledMagZ: "0.145305", scaledAmbientPressure: "1016.627380", deltaThetaX: "0.000141", deltaThetaY: "0.000108", deltaThetaZ: "-0.000096", deltaVelX: "0.003646", deltaVelY: "0.000563", deltaVelZ: "-0.093733", roll: "-0.005220", pitch: "0.042930", yaw: "0.558326", orientQuaternion: "[0.961046,-0.00842277,0.0199133,0.27554]", orientMatrix: "[[0.847362,0.529279,-0.0429168],[-0.52995,0.848013,-0.0052155],[0.0336336,0.0271632,0.999065]]", stabilizedMagX: "0.325882", stabilizedMagY: "-0.208165", stabilizedMagZ: "0.148896", stabilizedAccelX: "0.042812", stabilizedAccelY: "0.005203", stabilizedAccelZ: "-0.996619", gpsCorrelTimestampTow: "309057.015000", gpsCorrelTimestampWeekNum: "2245", gpsCorrelTimestampFlags: "5"},
{time: "2014-01-01 00:22:01.200000000", scaledAccelX: "0.038701", scaledAccelY: "0.007346", scaledAccelZ: "-0.997266", scaledGyroX: "0.001427", scaledGyroY: "0.001284", scaledGyroZ: "-0.001059", scaledMagX: "0.326149", scaledMagY: "-0.211073", scaledMagZ: "0.147229", scaledAmbientPressure: "1016.640015", deltaThetaX: "0.000049", deltaThetaY: "0.000060", deltaThetaZ: "-0.000060", deltaVelX: "0.001957", deltaVelY: "0.000418", deltaVelZ: "-0.049938", roll: "-0.005225", pitch: "0.042927", yaw: "0.558373", orientQuaternion: "[0.96104,-0.00842497,0.0199111,0.275563]", orientMatrix: "[[0.847337,0.529319,-0.042914],[-0.52999,0.847988,-0.00521991],[0.0336276,0.027167,0.999065]]", stabilizedMagX: "0.325892", stabilizedMagY: "-0.208194", stabilizedMagZ: "0.148905", stabilizedAccelX: "0.042810", stabilizedAccelY: "0.005207", stabilizedAccelZ: "-0.996652", gpsCorrelTimestampTow: "309057.065000", gpsCorrelTimestampWeekNum: "2245", gpsCorrelTimestampFlags: "5"},

I want to split the lines and convert each variable to columns with respective column names?

答案1

得分: 1

import pandas as pd

d = {"time": "2014-01-01 00:22:01.150000000", 
     "scaledAccelX": "0.039298", 
     "scaledAccelY": "0.005923", 
     "scaledAccelZ": "-0.994687", 
     # .... 
     "gpsCorrelTimestampTow": "309057.015000", 
     "gpsCorrelTimestampWeekNum": "2245", 
     "gpsCorrelTimestampFlags": "5"}

d = str([d]).replace("'", '"')
df = pd.read_json(d, orient='records')

print(df)
英文:
import pandas as pd

d = {'time': "2014-01-01 00:22:01.150000000", 
     'scaledAccelX': "0.039298", 
     'scaledAccelY': "0.005923", 
     'scaledAccelZ': "-0.994687", 
     # .... 
     'gpsCorrelTimestampTow': "309057.015000", 
     'gpsCorrelTimestampWeekNum': "2245", 
     'gpsCorrelTimestampFlags': "5"}

d = str([d]).replace("'", '"')
df = pd.read_json(d, orient='records')

print(df)
                            time  ...  gpsCorrelTimestampFlags
0  2014-01-01 00:22:01.150000000  ...                        5

[1 rows x 31 columns]

Explanation

JSON is a strict format, you have to use " inside strings. So I use replace for that.

The string you provide is under the records format.

It's just missing list brackets

huangapple
  • 本文由 发表于 2023年3月12日 13:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定