读取文件的多种方式?

Multiple ways to read a file?

我不确定在下面第 2 行的两种情况下文件读取方式是否有区别。第一种情况在打开命令中有一个 'r',第二种情况才不是。两者输出相同的结果。这些只是实现相同结果的不同方法吗?

场景 1:

def readit(filename, astr):
    infile = open(filename, 'r')
    content = infile.read()
    infile.close()
    return content.count(astr)

print(readit("payroll.txt","Sue"))

场景 2:

def readit(filename, astr):
    infile = open(filename)
    content = infile.read()
    infile.close()
    return content.count(astr)

print(readit("payroll.txt","Sue"))

是的,这两段代码是等价的。 'r'open 的默认模式。来自 docs:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode.

infile = open(filename)    # by default it opens as read-only

没有区别

您可以考虑使用 with 打开文件,除其他好处外,它还可以自动关闭文件。最好逐行计算目标字符串,而不是将整个文件读入内存:

def readit(filename, astr):
    with open(filename) as infile:
        return sum(line.count(astr) for line in infile)

更短,更少内存,更多'Pythonic'


附带说明,line.count(astr) 将计算该子字符串的所有出现次数,即使是较大字符串的一部分。示例:

>>> s='she she he she he sheshe hehe'
>>> s.count('she')
5
>>> s.count('he')
9

考虑拆分文本以获得完整匹配:

>>> [word for word in s.split() if word=='she']
['she', 'she', 'she']
>>> [word for word in s.split() if word=='he']
['he', 'he']

或正则表达式:

>>> re.findall(r'\bshe\b', s)
['she', 'she', 'she']
>>> re.findall(r'\bhe\b', s)
['he', 'he']

来自 python 文档,是一样的。 但是如果你想增加可读性,最好添加 'r' 模式 :)