2017-03-07 111 views
-1

我正在使用python實現數據挖掘算法。我有一個大的csv文件,我用它作爲輸入文件來獲取項目集。我想通過程序將csv文件拆分成行。有人可以告訴如何使它成爲可能嗎?CSV行拆分

+0

看到熊貓圖書館,更準確地說read_csv() –

回答

0

我假設行由新行分隔,並且列由逗號分隔。在這種情況下,只是python已經知道如何逐行讀取它,在你的情況下意味着逐行讀取。然後每一行都可以在有逗號的地方分割。

item_sets=[] #Will put the data in here 

with open(filename, "r") as file: # open the file 
    for data_row in file: #get data one row at a time 
    # split up the row into columns, stripping whitespace from each one 
    # and store it in item_sets 
    item_sets.append( [x.strip() for x in data_row.split(",")]) 
+0

有很多實現..爲什麼要重新寫它? – nir0s

0
import csv 

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

將打印輸出一個CSV文件列表 的所有行我假設的read_csv大熊貓impelmentation更有效,但csv模塊內置於Python的,所以如果你不希望任何依賴關係,您可以使用它。