Python 2.7 您好,我有一个用python打印所需内容的代码,但我';I’我希望它将结果写入一个新文件

Python 2.7 您好,我有一个用python打印所需内容的代码,但我';I’我希望它将结果写入一个新文件,python-2.7,Python 2.7,该文件看起来像一系列带有ID的行: aaaa aass asdd adfg aaaa 我想在一个新文件中获取ID及其在旧文件中的出现情况,格式如下: aaaa 2 asdd 1 aass 1 adfg 1 使用由tab分隔的2个元素 我拥有的代码打印了我想要的内容,但没有写入新文件: with open("Only1ID.txt", "r") as file: file = [item.lower().replace("\n

该文件看起来像一系列带有ID的行:

aaaa  
aass  
asdd  
adfg  
aaaa  
我想在一个新文件中获取ID及其在旧文件中的出现情况,格式如下:

aaaa    2  
asdd    1  
aass    1  
adfg    1  
使用由tab分隔的2个元素

我拥有的代码打印了我想要的内容,但没有写入新文件:

with open("Only1ID.txt", "r") as file:
    file = [item.lower().replace("\n", "") for item in file.readlines()]
        for item in sorted(set(file)):
            print item.title(), file.count(item)

使用Python 2时,将控制台输出转换为文件输出的最简单方法是使用print chevron(
>
语法将输出重定向到任何类似文件的对象:

with open("filename", "w") as f:  # open a file in write mode
    print >> f, "some data"       # print 'into the file'
只需添加另一个
open
以打开输出文件并将V形符号添加到
print
语句中,您的代码就会如下所示:

with open("Only1ID.txt", "r") as file, open("output.txt", "w") as out_file:
    file = [item.lower().replace("\n", "") for item in file.readlines()]
    for item in sorted(set(file)):
        print >> out_file item.title(), file.count(item)
print >> out_file item.title(), file.count(item),  
但是,您的代码还有其他一些或多或少不应该做或可以改进的不好的事情:

  • 对于
    open
    返回的文件对象和处理过的字符串列表,不要使用相同的变量名
    file
    。这很混乱,只需使用两个不同的名称

  • 您可以直接迭代file对象,该对象的工作方式类似于以字符串形式返回文件行的生成器。生成器会及时处理下一个元素的请求,这意味着它不会像
    file.readlines()
    那样先将整个文件加载到内存中,然后再进行处理,而是在需要下一行时一次只读取和存储一行。这样可以提高代码的性能和资源效率

  • 如果您编写了一个列表理解,但不需要将其结果作为列表,因为您只是想使用
    for
    循环对其进行迭代,那么使用生成器表达式(与上面描述的文件对象的行生成器效果相同)会更有效。列表理解和生成器表达式之间唯一的语法区别是括号。将
    […]
    替换为
    (…)
    ,您就有了一个生成器。生成器唯一的缺点是,您既不能找出它的长度,也不能直接使用索引访问项目。由于您不需要这些功能中的任何一个,因此这里的生成器很好

  • 有一种更简单的方法可以从行中删除尾随的换行符:
    line.rstrip()
    删除所有尾随的空格。如果要保留例如空格,但只希望删除换行符,请将该字符作为参数传递:
    line.rstrip(“\n”)

    但是,在
    print
    调用过程中,如果不添加另一个隐式换行符,而不是先删除它,然后再重新添加,可能会更容易、更快。在Python 2中,只需在语句末尾添加一个逗号,即可抑制
    print
    的换行:

    with open("Only1ID.txt", "r") as file, open("output.txt", "w") as out_file:
        file = [item.lower().replace("\n", "") for item in file.readlines()]
        for item in sorted(set(file)):
            print >> out_file item.title(), file.count(item)
    
    print >> out_file item.title(), file.count(item),  
    
  • 有一个类型
    Counter
    来计算集合中元素的出现次数,这比自己编写要快和容易,因为您不需要为每个元素调用额外的
    count()
    计数器
    的工作方式基本上类似于一个字典,将项目作为键,将其计数作为值。只需从
    collections
    模块导入它,并像这样使用它:

    from collections import Counter
    c = Counter(lines)
    for item in c:
        print item, c[item]
    
    from collections import Counter
    with open("Only1ID.txt") as in_file, open("output.txt", "w") as out_file:
        counter = Counter(line.lower().rstrip("\n") for line in in_file)
        for item in sorted(counter):
            print >> out_file item.title(), counter[item]
    
应用了所有这些建议(不删除换行符的建议除外),并将变量重命名为更清晰的名称,优化后的代码如下所示:

from collections import Counter
c = Counter(lines)
for item in c:
    print item, c[item]
from collections import Counter
with open("Only1ID.txt") as in_file, open("output.txt", "w") as out_file:
    counter = Counter(line.lower().rstrip("\n") for line in in_file)
    for item in sorted(counter):
        print >> out_file item.title(), counter[item]