我有10个文件,其中有100个随机数,名称为randomnumbers(1-10).py。我想创建一个程序,当找到123的字符串时,会显示“恭喜”,并计算出现123的次数。我有“祝贺”部分,并且已经编写了计数部分的代码,但我总是得到零。怎么了?在Python中计算文件中的字符串
for j in range(0,10):
n = './randomnumbers' + str(j) + '.py'
s='congradulations'
z='123'
def replacemachine(n, z, s):
file = open(n, 'r')
text=file.read()
file.close()
file = open(n, 'w')
file.write(text.replace(z, s))
file.close()
print "complete"
replacemachine(n, z, s)
count = 0
if 'z' in n:
count = count + 1
else:
pass
print count
我的回答有用吗? – 2012-10-02 17:01:46