【发布时间】:2019-06-06 19:33:08
【问题描述】:
我在this link 中有一个名为 region_descriptions.json 的 JSON 文件。 此文件在我的 Windows 中的记事本 ++ 中未正确加载(因为它是一个巨大的文件)。该文件部分加载到谷歌浏览器中。这个文件是我的密集字幕任务的数据集,我需要编写一个 python 脚本来将其中的每个“短语”翻译成印地语。
我在 power shell 中导航到我的 json 文件所在的目录,然后使用以下命令设置环境变量:
>>$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Preeti\Downloads\Compressed\region_descriptions.json"
之后我尝试在同一目录中打开 jupyter notebook 并运行代码:
import ijson
from google.cloud import translate
translate_client = translate.Client()
parser = ijson.items(open("region_descriptions.json"), "item.regions.item")
maxTranslations = 100;
for region in parser:
translation = translate_client.translate(region["phrase"], target_language="hi")
print(region["phrase"])
print(translation['translatedText'])
maxTranslations-=1
if maxTranslations==0:
break
但是 jupyter notebook 给了我一个错误:
AttributeError Traceback (most recent call last)
<ipython-input-1-5fa13c6f3710> in <module>
2 from google.cloud import translate
3
----> 4 translate_client = translate.Client()
5
6 parser = ijson.items(open("region_descriptions.json"), "item.regions.item")
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\translate_v2\client.py in __init__(self, target_language, credentials, _http, client_info)
75 ):
76 self.target_language = target_language
---> 77 super(Client, self).__init__(credentials=credentials, _http=_http)
78 self._connection = Connection(self, client_info=client_info)
79
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\cloud\client.py in __init__(self, credentials, _http)
128 raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
129 if credentials is None and _http is None:
--> 130 credentials, _ = google.auth.default()
131 self._credentials = google.auth.credentials.with_scopes_if_required(
132 credentials, self.SCOPE
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in default(scopes, request)
303
304 for checker in checkers:
--> 305 credentials, project_id = checker()
306 if credentials is not None:
307 credentials = with_scopes_if_required(credentials, scopes)
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _get_explicit_environ_credentials()
163 if explicit_file is not None:
164 credentials, project_id = _load_credentials_from_file(
--> 165 os.environ[environment_vars.CREDENTIALS])
166
167 return credentials, project_id
c:\users\preeti\appdata\local\programs\python\python37\lib\site-packages\google\auth\_default.py in _load_credentials_from_file(filename)
100 # The type key should indicate that the file is either a service account
101 # credentials file or an authorized user credentials file.
--> 102 credential_type = info.get('type')
103
104 if credential_type == _AUTHORIZED_USER_TYPE:
AttributeError: 'list' object has no attribute 'get'
有人可以帮我编写一个 python 脚本来将 json 文件中的所有短语翻译成印地语或帮助我克服我的错误吗?我强烈建议从给出的链接下载 json 文件,以便更好地理解我所指的“短语”。
【问题讨论】:
标签: python json google-translate