下面的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发布于 2018-06-22 03:18:19
您可以尝试这样做,我已经修改了您的代码:
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 counthttps://stackoverflow.com/questions/50979449
复制相似问题