2016-04-27 115 views
1

Python新手在這裏。將CSV導入到列表Python中

我有一個包含在這種格式的數字的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 

我需要閱讀的文件,使得該列表會這樣

[ 
[[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]] 
] 

每一行的最後一個數字存儲在不同的列表中,如7中的情況。

我已經研究了一些答案,但發現它們作爲字符串存儲到列表中,這與我正在處理的問題不兼容。

非常感謝您的幫助。


+0

你所期望的最後四個項目的每一行被視爲整數或浮點數(93或93.0 )? – RafG

回答

0

沒有最簡單的方法使用外部模塊:

更新:我取代了簡單的float(...)轉換與嘗試生成一個浮動的新convert(...)方法,並返回原始的字符串(或可替換地可以做別的事情),而不是如果令牌拋出異常不是一個數字。

def convert(value_str): 
    try: # try to convert it to a float: 
     return float(value_str) 
    except ValueError: # if it is not a valid float literal, return the original string: 
     return value_str 

with open("file.csv") as csvfile: 
    split_lines = [line.split(",") for line in csvfile] 
    data = [[[convert(n) for n in line[:-1]], [convert(line[-1])]] for line in split_lines] 

    print(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] ] 
] 
+0

如果列表中的值是-nan,它會顯示錯誤嗎? – ethanruan

+0

是的,這假設只有有效的浮點數字用逗號分隔。您可以添加一個支票,例如如果轉換失敗,則返回值作爲字符串。添加到我的答案... –

+0

@ethanruan增加了一個convert()函數,用於處理令牌不是有效浮點數的情況。 –

3

你可以嘗試這樣的,

>>> 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]] 
+0

我認爲ethanruan想要存儲十進制數,所以添加轉換? – Whysmerhill

+0

在Python 3上,您需要'list(map(...))'或列表理解。 – RafG

+0

@Whysmerhill我已經添加了一個解決方案。謝謝 –

0

剛使用[]操作以獲取列表的左側和右側部分:

import csv 
... 
list = [] 
with open(filename, "rb") as fd: 
    reader = csv.reader(fd, delimiter = ",") 
    for row in reader: 
     left = list(map(lambda x: float(x), row[:-1])) 
     right = list(map(lambda x: float(x), row[-1:])) 
     list.append([ left, right ]) 
1

CSV libraries通常閱讀領域的字符串,所以你需要的字段顯式轉換。從csv模塊的文檔:

從csv文件讀取的每一行都以字符串列表形式返回。否 執行自動數據類型轉換。

>>> import csv 
>>> with open('eggs.csv', 'rb') as csvfile: 
...  spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|') 
...  for row in spamreader: 
...   <process row> 

同樣,CSV圖書館將平等對待所有領域,所以你需要明確地包裝在列表中的最後一個字段。

例如:

a = ["1.23", "2.34", "10", "100", "1000"] 

>>> map(float, a[0:2]) + map(int, a[2:4]) + [[int(a[4])]] 

[1.23, 2.34, 10, 100, [1000]] 
0

您需要遍歷列表,並將它們轉換爲整數。同樣以你想要的列表格式存儲它們。

例如:

import csv 
l = list() 
with open('data.csv', 'r') as csvfile: 
    reader = csv.reader(csvfile, delimiter=',') 
    for row in reader: 
    l.append([[float(row[:-1])]+[float(row[-1])]]) 
print(l) 
0

你可以試試這個, 考慮輸入文件名input.csv

import csv 
new_list = [] 
with open('input.csv') as inp: 
    csv_reader = csv.reader(inp, delimiter=',') 
    for line in csv_reader: 
     new_list.append([map(float, line[:-1])] + [map(float, line[-1:])]) 

從IPython中演示,

In [1]: import csv 

In [2]: new_list = [] 

In [3]: with open('input.csv') as inp: 
    ...:  csv_reader = csv.reader(inp, delimiter=',') 
    ...:  for line in csv_reader: 
    ...:   new_list.append([line[:-1]] + [line[-1:]]) 
    ...:   

In [4]: new_list 
Out[4]: 


    [[[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]]]