2009-11-30 66 views
0

我試圖編寫一個從包含包含列表的文件的「延遲」目錄中讀取文件的函數。下面是在延期文件夾中的文件包含:將列表從文件添加到Python中的單個列表中

'173378981', '45000', '343434', '3453453', '34534545', '3452342', '234234', '42063008', 'Exempted', '10000' 
'1000014833', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 
'1000009598', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 
'279483421', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 
'1000009600', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 
'389453080', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 
'1000009602', '0', '0', '0', '0', '0', '0', '0', 'Exempted', '0' 

用於寫文件(S)的功能:

def storeDeferredRecords(records): 
    """docstring for createFile""" 
    now = datetime.datetime.now() 
    filename = deferredDir + '/' + now.strftime("%Y%m%d-%H%M%S") 
    f = open(filename, 'w') 
    newlist = map(lambda(x): str(x)[1:-1], records) 
    for item in newlist: 
     f.write("%s\n" % item) 
    f.close 

我需要用來讀取文件中的函數幫助。我只能寫這個:

def getDeferredRecords(): 
     """docstring for getDeferredRecords""" 
     infiles = [infile for infile in glob.glob(deferredDir + '/*')] 
       <code to read the contents of each file here> 

有人可以幫我嗎?我需要讀取這些行並將它們插入到列表中。此列表將與來自單獨的CSV文件的記錄合併。

+0

這功課嗎? – 2009-11-30 15:10:09

回答

1

the csv module

BigList = [] 
for filename in glob.glob(deferredDir + '/*'): 
    PartList = csv.reader(open(filename)) 
    BigList.extend(PartList) 

是你腦子裏想的是什麼?

+0

謝謝蒂姆。我已經在使用CSV模塊來讀取初始源文件。 「延遲」文件夾內的文件是從初始源文件創建的。 – Francis 2009-11-30 15:24:11

+0

是的,這是我的想法。也感謝讓我意識到我可以使用csv。讀者也可以將列表加載到「大名單」中。 – Francis 2009-11-30 15:42:16

1

Python的cvs模塊可能是一個很好的答案:
http://docs.python.org/library/csv.html

問:

glob.glob()已經返回一個迭代,所以我在這裏看不到的點...

[infile for infile in glob.glob(deferredDir + '/*')] 

相反:

BigList = [] 
for filename in glob.glob(deferredDir + '/*'): 
    #CVS read code here 
    #add to BigList 

思考的食物。

+0

感謝您指出這一點!但是,「延遲」目錄內會有X個文件。我需要瀏覽每個文件,將內容讀入列表中,然後將其附加到大列表中。 – Francis 2009-11-30 15:22:44

2

首先,在存儲功能中的最後一行需要像這樣f.close()

你的存儲功能在一個換行符分隔的方式保存的值。要閱讀所有的文件,應該是足夠了:

def getDeferredRecords(): 
    """docstring for getDeferredRecords""" 
    return dict((infile, list(iter(file(infile)))) 
        for infile in glob.glob(deferredDir + '/*')) 

說明:文件是可迭代的,所以你可以例如做for line in file: print line。用list(iter(file))你有一個列表中的文件的行。 dict((a, b) for a, b in foo)返回一個包含{a: b}對的字典。函數的返回值是格式爲{filename: list_of_lines_in_file}的字典。請記住,列表元素是具有尾隨換行符的字符串。

+0

嗨奧托。代碼返回爲「無效語法」 – Francis 2009-11-30 15:39:43

+0

用'''替換'''返回代碼((infile,list(iter(infile))'''返回代碼((infile,list(iter(infile)))''' – Abgan 2009-11-30 15:55:01

+0

* sigh *總是有一些東西 - 用'file(infile)'和固定圓括號代替'infile' - 謝謝abgan – 2009-11-30 16:18:24

0

蒂姆Pietzcker合併的想法,這裏有重新編寫的函數:

def storeDeferredRecords(records): 
    """docstring for createFile""" 
    now = datetime.datetime.now() 
    filename = deferredDir + '/' + now.strftime("%Y%m%d-%H%M%S") 
    f = csv.writer(open(filename, 'w'), delimiter=',') 
    f.writerows(records) 

def getDeferredRecords(): 
    """docstring for getDeferredRecords""" 
    for filename in glob.glob(deferredDir + '/*'): 
     def_records = csv.reader(open(filename,'r')) 
     records.extend(def_records) 

我來替代使用以前的代碼塊csv.writer:

f = open(filename, 'w') 
newlist = map(lambda(x): str(x)[1:-1], records) 
for item in newlist: 
     f.write("%s\n" % item) 
f.close 

感謝所有那些誰回答!

+0

今天學習了2個新的課程:你可以使用list .extend(list)to「append」list to another list and use csv.writer instead of written your own function to write a comma-separated list to a file。 – Francis 2009-11-30 16:31:43

+0

也感謝@gahooa指出我不再需要使用列表理解來遍歷使用glob.glob()的目錄。 – Francis 2009-12-01 02:24:13

相關問題