【问题标题】:Appending lists from files to a single list in Python将文件中的列表附加到 Python 中的单个列表
【发布时间】:2009-11-30 15:05:26
【问题描述】:

我正在尝试编写一个函数,该函数从“延迟”目录中读取文件,该目录包含包含列表的文件。以下是 deferred 文件夹中的文件包含的内容:

'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'

用于写入文件的函数:

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 文件的记录合并。

【问题讨论】:

    标签: python list io


    【解决方案1】:

    首先,store函数的最后一行需要是这样的f.close()

    您的 store 函数以换行符分隔的方式保存值。要读取所有文件,应该足够了:

    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} 的字典。请记住,列表元素是带有尾随换行符的字符串。

    【讨论】:

    • 替换 '''return dict((infile, list(iter(infile))''' 为 '''return dict((infile, list(iter(infile)))''' 对list(iter(infile)) 后缺少括号。
    • sigh 总有一些东西 - 将 infile 替换为 file(infile) 和固定括号 - 感谢 abgan
    【解决方案2】:

    the csv module:

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

    这就是你的想法吗?

    【讨论】:

    • 谢谢蒂姆。我已经在使用 CSV 模块来读取初始源文件。 “deferred”文件夹中的文件是从初始源文件创建的。
    • 是的,这就是我的想法。也感谢您让我意识到我也可以使用 csv.reader 将列表加载到“大列表”中。
    【解决方案3】:

    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
    

    值得深思。

    【讨论】:

    • 感谢您指出这一点!但是,“延迟”目录中将有 X 个文件。我需要遍历每个文件,将内容读取到列表中,然后将其附加到大列表中。
    【解决方案4】:

    结合 Tim 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
    

    感谢所有回复的人!

    【讨论】:

    • 今天学到了 2 个新课程:您可以使用 list.extend(list) 将列表“附加”到另一个列表并使用 csv.writer 而不是编写自己的函数来编写逗号分隔的列表到一个文件。
    • 也感谢@gahooa 指出我不再需要使用列表推导来使用 glob.glob() 遍历目录。
    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 2019-03-29
    • 2014-05-13
    • 2017-12-18
    • 2021-07-12
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    相关资源
    最近更新 更多