Reading multiple lines from an external text file in Python
当我使用代码时,这个程序工作正常
1 | for character in infile.readline(): |
问题是readline只读取一行文本。当我将"s"添加到readline命令时
1 | for character in infile.readlines(): |
我最终得到了0的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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() |
另外,如果有人能给我建议,我能把这个目录设为默认位置吗?这样我就可以把它放在别人的机器上使用。
你可以使用两个形式
1 2 3 4 5 6 7 8 9 10 11 | 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}) |
然后
如果你只想检查的类型的caracters contained在文件,我不会被使用,但readlines定期读一读。 P / < >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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() |
如果你真的需要使用
为你的assuming审,
1 2 | this Is a TEST and this is another line |
用
1 2 | ['this Is a TEST ', 'and this is another line'] |
*在所有的,你可以走的文件的内容使用的双
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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() |
20世纪的农药目录的事,如果你的代码和你的文本文件(
让我们说你船在它的目录:像 P / < >
1 2 3 | |-> Count |-> script.py |-> module3.txt |
你可以只使用相对paths到开放,从
就这样,改变你的hardcoded
1 2 | print"Changing current directory to %s" % os.path.dirname(os.path.realpath(__file__)) os.chdir(os.path.dirname(os.path.realpath(__file__))) |