写一个程序来处理“pc\u woodchuck.txt”的内容 一行一行。它在当前工作目录中创建一个名为 “pc\u woodchuck.tmp”,它的内容与“pc\u woodchuck.txt”相同,只是所有元音都被删除了(不区分大小写)。最后,显示你读了多少个字符,写了多少个字符
Hoeveel hout kan een houthakker hakken Hoeveel hout kan een houthakker hakken
你是说你是谁?
Hij kan hakken zoveel als你好,你好
你知道吗?你知道吗
我也不知道
from os.path import join
from os import getcwd
def removeVowels( line ):
newline = ""
for c in line:
if c not in "aeiouAEIOU":
newline += c
return newline
inputname = join( getcwd(), "pc_woodchuck.txt" )
outputname = join( getcwd(), "pc_woodchuck.tmp" ) # This will be the copy of the
# textfile at 'inputname' without
# vowels (pc_woodchuck.tmp).
fpi = open( inputname )
fpo = open( outputname, "w" )
countread = 0
countwrite = 0
while True:
line = fpi.readline()
if line == "":
break
countread += len( line )
line = removeVowels( line )
countwrite += len( line )
fpo.write( line )
fpi.close()
fpo.close()
print( "Read:", countread )
print( "Wrote:", countwrite )
Read: 201
Wrote: 2 # But there must be more than two vowels!
我做错什么了?结果“写道:2”显然不对
return newline
在for循环中,因此函数在第一个循环中返回,这就是为什么只有一个字母我想应该是:
相关问题 更多 >
编程相关推荐