我正在尝试将数据,列表转换为csv文件。
列表名为 data
Model
csv文件名为 csv ,应该如下所示:
['time\ntimestamp\nrecordtime\nlat\nlong\nalt\nspeed\ncourse\nverticalAccuracy\nhorizontalAccuracy\nlocTimeStamp\naccelerationX\naccelerationY\naccelerationZ\nHeadingX\nHeadingY\nHeadingZ\nTrueHeading\nMagneticHeading\nHeadingAccuracy\nRotationX\nRotationY\nRotationZ\nmotionYaw\nmotionRoll\nmotionPitch\nmotionRotationRateX\nmotionRotationRateY\nmotionRotationRateZ\nmotionUserAccelerationX\nmotionUserAccelerationY\nmotionUserAccelerationZ\nen0\npdp_ip0\nDeviceOrientation\nState', '2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;0.0.0.0;10.167.22.1;1;0','2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;0.0.0.0;10.167.22.1;1;0']
换句话说,time,timestamp,recordtime,lat,long,alt,...,State
2014-11-19 08:57:45.696, 1, 1, 52.33705, 4.843468, 8.319668,..,0
2014-11-19 08:57:45.696, 1, 1, 52.33705, 4.843468, 8.319668,..,0
必须使用换行符替换为'\n'
和','
,并且这些行','
必须为';'
。我不知道从哪里开始。
答案 0 :(得分:2)
让l
成为您的清单。
>>> header = l[0].split('\n')
>>> rows = [s.split(';') for s in l[1:]]
演示:
>>> header[:3]
['time', 'timestamp', 'recordtime']
>>> rows[0][:3]
['2014-11-19 08:57:45.696', '1', '1']
>>> rows[1][:3]
['2014-11-19 08:57:45.696', '1', '1']
我将文件I / O的任务留给您,因为这是问题的简单部分。找到csv
模块的文档没有问题,我们不会在这里编写完整的代码。
答案 1 :(得分:1)
您的数据中实际上没有逗号 - 这些是由新行或半列分隔的列表索引,因此您只需将每个元素拆分为单个单元格,最后将所有内容写入标准CSV格式,您可以使用内置的csv
模块执行此操作:
your_list = ['time\ntimestamp\nrecordtime\nlat\nlong\nalt\nspeed\ncourse\nverticalAccuracy'
'\nhorizontalAccuracy\nlocTimeStamp\naccelerationX\naccelerationY'
'\naccelerationZ\nHeadingX\nHeadingY\nHeadingZ\nTrueHeading\nMagneticHeading'
'\nHeadingAccuracy\nRotationX\nRotationY\nRotationZ\nmotionYaw\nmotionRoll'
'\nmotionPitch\nmotionRotationRateX\nmotionRotationRateY\nmotionRotationRateZ'
'\nmotionUserAccelerationX\nmotionUserAccelerationY\nmotionUserAccelerationZ'
'\nen0\npdp_ip0\nDeviceOrientation\nState',
'2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;'
'1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;'
'53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;'
'0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;'
'0.0.0.0;10.167.22.1;1;0',
'2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;'
'1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;'
'53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;'
'0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;'
'0.0.0.0;10.167.22.1;1;0']
# On Python 3.x use open("output.csv", "wt", newline="") instead
with open("output.csv", "wb") as f: # open output.csv for writing
writer = csv.writer(f) # create a CSV writer
for element in your_list: # iterate over each element in your_list
# turn each line into cells by splitting first by a new line, then by a semi-column
# then write the resulting cells to the CSV using our CSV writer
writer.writerow([v for e in element.split("\n") for v in e.split(";")])
结果output.csv
将包含:
time,timestamp,recordtime,lat,long,alt,speed,course,verticalAccuracy,horizontalAccuracy,locTimeStamp,accelerationX,accelerationY,accelerationZ,HeadingX,HeadingY,HeadingZ,TrueHeading,MagneticHeading,HeadingAccuracy,RotationX,RotationY,RotationZ,motionYaw,motionRoll,motionPitch,motionRotationRateX,motionRotationRateY,motionRotationRateZ,motionUserAccelerationX,motionUserAccelerationY,motionUserAccelerationZ,en0,pdp_ip0,DeviceOrientation,State 2014-11-19 08:57:45.696,1,1,52.33705,4.843468,8.319668,20.44,93.05875,6,5,1416383865,0.0806427,-0.4734802,-0.7419281,-16.14367,27.67606,-15.92525,53.06408,52.48165,26,0.04704554,0.9128267,0.4871223,0.1783603,0.2536548,0.6368796,0.07435028,0.8879893,0.4615479,-0.1116289,0.1097959,0.04115437,0.0.0.0,10.167.22.1,1,0 2014-11-19 08:57:45.696,1,1,52.33705,4.843468,8.319668,20.44,93.05875,6,5,1416383865,0.0806427,-0.4734802,-0.7419281,-16.14367,27.67606,-15.92525,53.06408,52.48165,26,0.04704554,0.9128267,0.4871223,0.1783603,0.2536548,0.6368796,0.07435028,0.8879893,0.4615479,-0.1116289,0.1097959,0.04115437,0.0.0.0,10.167.22.1,1,0
答案 2 :(得分:1)
此代码可以解决您的问题:
import csv
def csv_writer(data, path):
with open(path, "a",newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(data)
data = ['time\ntimestamp\nrecordtime\nlat\nlong\nalt\nspeed\ncourse\nverticalAccuracy\nhorizontalAccuracy\nlocTimeStamp\naccelerationX\naccelerationY\naccelerationZ\nHeadingX\nHeadingY\nHeadingZ\nTrueHeading\nMagneticHeading\nHeadingAccuracy\nRotationX\nRotationY\nRotationZ\nmotionYaw\nmotionRoll\nmotionPitch\nmotionRotationRateX\nmotionRotationRateY\nmotionRotationRateZ\nmotionUserAccelerationX\nmotionUserAccelerationY\nmotionUserAccelerationZ\nen0\npdp_ip0\nDeviceOrientation\nState', '2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;0.0.0.0;10.167.22.1;1;0','2014-11-19 08:57:45.696;1;1;52.33705;4.843468;8.319668;20.44;93.05875;6;5;1416383865;0.0806427;-0.4734802;-0.7419281;-16.14367;27.67606;-15.92525;53.06408\n52.48165;26;0.04704554;0.9128267;0.4871223;0.1783603;0.2536548;0.6368796;0.07435028;0.8879893;0.4615479;-0.1116289;0.1097959;0.04115437;0.0.0.0;10.167.22.1;1;0']
for d in data:
csv_writer(d.replace('\n',',').replace(';',',').split(','),'data.csv')