2009-11-30 76 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

相关问题