当我使用代码时,这个程序工作正常
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()
另外,如果有人能给我建议,我能把这个目录设为默认位置吗?这样我就可以把它放在别人的机器上使用。
f.readlines()读取所有行。对于目录,可以将其作为参数传递给脚本。而if something == True:应为if something:。
你可以使用两个形式iter读到他们的一个arbitrary的字节数美元的时间,itertools.chain到认为他们是个长的输入。代替保持轨道的several变量的描述,你可以使用的方法strAS键到collections.Counter,EG: P / < >
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({: 39, : 10, : 0, : 0})
然后counts[str.lower]会给你39为审……………… P / < >
如果你只想检查的类型的caracters contained在文件,我不会被使用,但readlines定期读一读。 P / < >
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,保持在所有的方法,就是将所有的读,有英语和文件放在他们的记忆(不那么好,与……非常大的文件)在名单的线。 P / < >
为你的assuming审,module3.txtcontains文件: P / < >
this Is a TEST
and this is another line
用readlines()将回报: P / < >
['this Is a TEST
', 'and this is another line']
*在所有的,你可以走的文件的内容使用的双for环: P / < >
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世纪的农药目录的事,如果你的代码和你的文本文件(module3.txt)是要去shipped在相同的目录,你不需要做的chdir。通过违约,"工作目录"的脚本,在脚本的目录。 P / < >
让我们说你船在它的目录:像 P / < >
|-> Count
|-> script.py
|-> module3.txt
你可以只使用相对paths到开放,从module3.txt在script.py:线* open("module3.txt","r")会去看的文件称为module3.txtwithing的目录在"脚本可以运行(意义,Count\)。你不需要的呼叫到os.chdir。如果你仍然想让知道,你可以chdir目录在"脚本,也located(带的看看这个:) P / < >
就这样,改变你的hardcoded chdir线(os.chdir(r'M:\Project\Count')在顶上的文件到你的): P / < >
print"Changing current directory to %s" % os.path.dirname(os.path.realpath(__file__))
os.chdir(os.path.dirname(os.path.realpath(__file__)))