繁体   English   中英

Python初学者-如何将几个文件的内容读入唯一列表?

[英]python beginner - how to read contents of several files into unique lists?

我想将几个文件中的内容读入一个唯一的列表中,以便以后使用-最终,我想将这些列表转换为集合并对其进行相交和相减。 这肯定是一个非常幼稚的问题,但是仔细研究了Lutz的“学习Python”的迭代器和循环部分之后,我似乎无法全神贯注于如何实现这一点。 这是我写的:

#!/usr/bin/env python

import sys

OutFileName = 'test.txt'
OutFile = open(OutFileName, 'w')

FileList = sys.argv[1: ]
Len = len(FileList)
print Len

for i in range(Len):
    sys.stderr.write("Processing file %s\n" % (i))
    FileNum = i

for InFileName in FileList:
    InFile = open(InFileName, 'r')
    PathwayList = InFile.readlines()
    print PathwayList
    InFile.close()

通过几个简单的测试文件,我得到如下输出:

处理文件0

处理文件1

['alg1 \\ n','alg2 \\ n','alg3 \\ n','alg4 \\ n','alg5 \\ n','alg6']

['csr1 \\ n','csr2 \\ n','csr3 \\ n','csr4 \\ n','csr5 \\ n','csr6 \\ n','csr7 \\ n','alg2 \\ n',' alg6']

这些列表是正确的,但是如何将每个列表分配给一个唯一变量,以便以后可以调用它们(例如,通过在变量名称中包含来自range的索引号)?

非常感谢您为正确的方向指明了一个完整的编程初学者!

#!/usr/bin/env python

import sys

FileList = sys.argv[1: ]
PathwayList = []
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % (i))
    InFile = open(InFileName, 'r')
    PathwayList.append(InFile.readlines())
    InFile.close()

假设您读了两个文件,下面的代码将逐行进行比较(它不会在较长的文件中占用任何多余的行,但是如果一个行比另一个行多,它们将是不同的;)

for i, s in enumerate(zip(PathwayList[0], PathwayList[1]), 1):
    if s[0] == s[1]:
        print i, 'match', s[0]
    else:
        print i, 'non-match', s[0], '!=', s[1]

对于您想做的事情,您可能想看一下Python中的difflib模块。 为了进行排序,请查看Mutable Sequence TypessomeListVar.sort()将在适当位置对someListVar的内容进行排序。

如果不需要记住内容的来源,则可以这样做:

PathwayList = []
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList.append(InFile.readlines())
    InFile.close()  

for contents in PathwayList:
    # do something with contents which is a list of strings
    print contents  

或者,如果您想跟踪文件名,则可以使用字典:

PathwayList = {}
for InFileName in FileList:
    sys.stderr.write("Processing file %s\n" % InFileName)
    InFile = open(InFileName, 'r')
    PathwayList[InFile] = InFile.readlines()
    InFile.close()

for filename, contents in PathwayList.items():
    # do something with contents which is a list of strings
    print filename, contents  

您可能想检查Python的fileinput模块,它是标准库的一部分,并允许您一次处理多个文件。

本质上,您具有文件列表,并且想要更改为这些文件的行列表...

几种方法:

result = [ list(open(n)) for n in sys.argv[1:] ]

这将为您提供-> [[['alg1','alg2','alg3'],['csr1','csr2'...]]的结果,访问将类似于'result [0]'在['alg1','alg2','alg3']中...

字典可能更好一些:

result = dict( (n, list(open(n))) for n in sys.argv[1:] )

如果只想串联,则只需将其链接:

import itertools
result = list(itertools.chain.from_iterable(open(n) for n in sys.argv[1:]))
# -> ['alg1', 'alg2', 'alg3', 'csr1', 'csr2'...

对于初学者来说不是一线希望...但是现在,尝试了解正在发生的事情将是一个不错的选择:)

您需要为正在读取的每个文件“编号”动态创建变量名。 (我故意含糊不清,知道如何构建这样的变量是非常有价值的,如果您自己发现它,更容易记住)

这样会给你一个开始

您需要一个包含PathwayList列表的列表,即列表列表。

一句话:使用大写的变量名是很不常见的。 对此没有严格的规定,但是按照惯例,大多数人只对类使用大写名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM