下面的Python程序检查字符串中是否存在字母,如果不存在字母,则使用自定义API将其转换为英语并将其写入文件。由于isalpha()检查- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'。我不确定为什么程序会为此字符串输入第一个循环- '龙海德信机电有限公司'。当我运行调试器时,它表明该isalpha()函数的求值结果龙是字母。我不确定为什么会这样。def translate_function(file): filea = open(file,encoding = "utf8") fileb = open("lmao.txt", 'r+') count = 0 for i in filea: state = 'false' count += 1 for j in i : if (j.isalpha()): state = 'true' print(i, "This is English") break if (state == 'false'): trans = translate(i) fileb.write(trans) fileb.write('\n') return count
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
您可以尝试一下,我对您的代码做了一些修改:
def translate_function(file):
filea = open(file,encoding = "utf8")
fileb = open("lmao.txt", 'r+')
count = 0
for i in filea:
state = 'false'
count += 1
words = i.split(" ")
for word in words:
if not word.isalpha():
trans = translate(i)
fileb.write(trans)
fileb.write('\n')
return count
添加回答
举报
0/150
提交
取消