2013-05-21 15 views
-1

不知道從哪裏開始這...我知道如何閱讀一個csv文件,但如果我有一堆文件在同一目錄中,如何可以根據是否閱讀它們是否他們在列表中。例如,如列表...循環列表和打開文件,如果找到

l= [['file1.csv','title1','1'], ['file2.csv','title2','1'],['file3.csv','title3','1']] 

我怎樣才能得到只是那些3個文件,即使我最多在目錄「file20.csv」。 我可以以某種方式遍歷列表並使用if語句來檢查文件名並在找到時打開文件?

+0

您想使用哪種編程語言? – reporter

+0

oops。 Python 2.x –

回答

0
for filedesc in l: #go over each sublist in l 
    fname, ftitle, _ = filedesC#unpack the information contained in it 
    with open(fname) as f: #open the file with the appropriate name 
     reader = csv.reader(f) #create reader of that file 
     #go about bussiness 
+0

謝謝@HennyH,但我可以問,我需要解壓所有的信息,或不能我搜索出現在第一列即文件名。 file1.csv。 –

0

更新的職位,因爲我已經變得如此接近這個....

lfiles= [] 
csvfiles=[] 
for row in l: 
    lfiles= row[0]  #This reads in just the filesnames from list 'l' 
with open(lfiles) as x: 
    inread = csv.reader(x) 
    for i in x: 
     print i 

這是在宣讀的文件打印的一切,但我現在想追加「csvfiles」 (一個空列表)與一行,如果一個特定的列等於某事。 可能是這樣的...... ????

for i in x: 
    for line in i: 
     if line= 'ThisThingAppears5Times': 
      csvfiles.append(line) # and now the 5 lines are in a 2dlist 

當然,這不行,但關閉??