在Python中从外部文本文件中读取多行

时间:2014-04-06 17:12:57

标签: python text-files readline

当我使用代码时,这个程序工作正常

for character in infile.readline():  

问题是readline只读取一行文本。当我将“s”添加到readline命令

for character in infile.readlines():  

我最终得到0是我的输出。

os.chdir(r'M:\Project\Count')

def main():
    infile = open("module3.txt","r")
    uppercasecount = 0
    lowercasecount = 0
    digitcount = 0
    spacecount = 0
    for character in infile.readlines():
        if character.isupper() == True:
            uppercasecount += 1
        if character.islower() == True:
            lowercasecount += 1
        if character.isdigit() == True:
            digitcount += 1
        if character.isspace() == True:
            spacecount += 1

    print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))

main()

此外,如果有人可以给我建议,我可以将该目录作为默认位置,这样我就可以在其他人的机器上使用它。

2 个答案:

答案 0 :(得分:2)

您可以使用双格式iter一次读取任意数量的字节,itertools.chain将它们视为一个长输入。您可以使用str方法作为collections.Counter的键,而不是跟踪多个变量,例如:

from collections import Counter
from itertools import chain

counts = Counter()
with open('yourfile') as fin:
    chars = chain.from_iterable(iter(lambda: fin.read(4096), ''))
    for ch in chars:
        for fn in (str.isupper, str.islower, str.isdigit, str.isspace):
            counts[fn] += fn(ch)

#Counter({<method 'islower' of 'str' objects>: 39, <method 'isspace' of 'str' objects>: 10, <method 'isdigit' of 'str' objects>: 0, <method 'isupper' of 'str' objects>: 0})

然后counts[str.lower]会给你39例如......

答案 1 :(得分:1)

如果您只是想检查文件中包含的caracters的类型,我不会使用readlines而是常规read

STEP_BYTES = 1024

def main():
    infile = open("module3.txt","r")
    uppercasecount = 0
    lowercasecount = 0
    digitcount = 0
    spacecount = 0
    data = infile.read(STEP_BYTES)
    while data:
        for character in data:
            if character.isupper() == True:
                uppercasecount += 1
            if character.islower() == True:
                lowercasecount += 1
            if character.isdigit() == True:
                digitcount += 1
            if character.isspace() == True:
                spacecount += 1
        data = infile.read(STEP_BYTES)

    print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))

main()

如果您确实需要使用readlines,请记住该方法将读取文件的所有行并将其放入内存列表中(对于非常大的文件来说不太好)。

例如,假设您的module3.txt文件包含:

this Is a TEST
and this is another line

使用readlines()将返回:

['this Is a TEST\n', 'and this is another line']

考虑到这一点,您可以使用双for循环遍历文件内容:

def main():
    infile = open("module3.txt","r")
    uppercasecount = 0
    lowercasecount = 0
    digitcount = 0
    spacecount = 0
    lines = infile.readlines()
    for line in lines:
        for character in line:
            if character.isupper() == True:
                uppercasecount += 1
            if character.islower() == True:
                lowercasecount += 1
            if character.isdigit() == True:
                digitcount += 1
            if character.isspace() == True:
                spacecount += 1
    print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))

main()

至于目录事物,如果您的代码和文本文件(module3.txt)将在同一目录中提供,则您不需要执行chdir。默认情况下,脚本的工作目录是脚本所在的目录。

我们假设您将其发送到如下目录:

  |-> Count
     |-> script.py
     |-> module3.txt

您可以使用相对路径从module3.txt内打开script.py:行open("module3.txt", "r")将查找名为module3.txt的文件,其中包含脚本所在的目录跑步(意思是Count\)。您不需要拨打os.chdir。如果您仍想确定,可以chdir到脚本所在的目录(请查看this):

知道这一点,将您的硬编码chdir行(文件顶部为os.chdir(r'M:\Project\Count'))更改为:

print "Changing current directory to %s" % os.path.dirname(os.path.realpath(__file__))
os.chdir(os.path.dirname(os.path.realpath(__file__)))
相关问题