尝试使用 Py 进行自动文本文件翻译时出现 NoneType 错误

问题描述

我正在尝试开发能够接受 txt 文件并将其中的内容翻译成我提供的语言的东西。我对此做了一个小测试,因为我不使用 python,但在尝试翻译时出现此错误

AttributeError: 'nonetype' 对象没有属性 'group'"

代码

import re
import googletrans
from googletrans import Translator

file_translator = Translator()

tFile = open('demo.txt','r')

if tFile.mode == 'r':
    content = tFile.read()
    print(content)

result = file_translator.translate(content,dest='fr')

with open('output.txt','w') as outFile:
    outFile.write(result.text)

解决方法

这是3.0.0版本的问题。您需要安装最新版本:

pip install googletrans==4.0.0-rc1

或者,如果您使用 PyCharm,您可以在这里进行: enter image description here enter image description here

,

此问题已在官方存储库中报告。见this issue

对于任何接收到 NoneType' 对象的人来说,它都没有属性 'group,如果你 目前正在使用 googletrans==3.0.0,请切换到 googletrans==3.1.0a0 用于临时修复。

此修复对某些人不起作用。如果它对您不起作用,请尝试安装版本 4.0.0rc1

pip install googletrans==4.0.0rc1