你可以嘗試這樣的,
>>> csv = '''9143.680696, 427953.500000, 11919.104475, 11908.727555,
1.000871, 0.029506, 15.546608, 93, 121, 123, 7
7704.773182, 330297.500000, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93, 121, 123, 7'''
>>> [[line.split(',')[0:-1], [line.split(',')[-1]]] for line in csv.splitlines()]
[[['9143.680696', ' 427953.500000', ' 11919.104475', ' 11908.727555', ' 1.000871', ' 0.029506', ' 15.546608', ' 93', ' 121', ' 123'], [' 7']], [['7704.773182', ' 330297.500000', ' 19186.759308', ' 19170.146116', ' 1.000867', ' 0.029426', ' 14.302257', ' 93', ' 121', ' 123'], [' 7']]]
如果你想float
項目,你可以使用map
,
>>> data = csv.splitlines()
>>> data = [map(float, line.split(',')) for line in csv.splitlines()]
>>> [[items[:-1], items[-1]] for items in data]
[[[9143.680696, 427953.5, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93.0, 121.0, 123.0], 7.0], [[7704.773182, 330297.5, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93.0, 121.0, 123.0], 7.0]]
漂亮的印刷:
>>> import pprint
>>> pprint.pprint([[items[:-1], items[-1]] for items in data])
[[[9143.680696,
427953.5,
11919.104475,
11908.727555,
1.000871,
0.029506,
15.546608,
93.0,
121.0,
123.0],
7.0],
[[7704.773182,
330297.5,
19186.759308,
19170.146116,
1.000867,
0.029426,
14.302257,
93.0,
121.0,
123.0],
7.0]]
你所期望的最後四個項目的每一行被視爲整數或浮點數(93或93.0 )? – RafG