2013-09-27 45 views
0
outputDatafile = open('D:\\Files\\input.csv', 'r') 
outputReader = csv.reader(outputDatafile, delimiter=',', quoting=csv.QUOTE_NONE) 

print "OutputReader data type:",type(outputReader) 
inputData= [] 
//i want to extract all values in csv to array 
for row in outputReader: 
    inputData.append(row) 

我有一個CSV文件,如下所示:如何提取CSV文件和存儲的話,列出在Python

名1,value11,value12,value13

名2,value21,value22, value23,value24

NAME3

我想提取和存儲所有項目列出 如:

list[0]=name1 
list[1]=value11 
list[2]=value12 
. 
. 
. 
. 
list[5]=name2 
list[6]=value21 

回答

0

使用extend(),而不是append()。後者將每個列表添加爲外部列表中的一個元素,前者連接列表。您也可以使用+=

with open(r'D:\Files\input.csv') as f: 
    input_data = [] 
    for row in csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE): 
     input_data += row 
0

.csv可以被視爲與分離器「」所以,你需要做的唯一事情是分裂像串正常txt文件:

outputDatafile = open('D:\\Files\\input.csv', 'r') 

inputData= [] 
for rows in outputDatafile: 
    row = rows.rstrip().spilt(',')  #row is a list of strings splited by ',' of rows. 
             #and row[0] is name1 and so on 
相關問題