Functionality of isalpha function
下面的Python程序检查字符串中是否存在字母,如果不存在字母,则使用自定义API将其转换为英语并将其写入文件。 由于
我不确定为什么程序会为此字符串输入第一个循环-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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('\ ') return count |
您可以尝试一下,我对您的代码做了一些修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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('\ ') return count |