У меня есть файл JSON с именем region_description.json, доступный в по этой ссылке .
Этот файл не загружается должным образом в notepad ++ в моих окнах (так как это огромный файл). Файл частично загружается в Google Chrome. Этот файл является набором данных для моей задачи с плотными заголовками, и мне нужно написать скрипт на python для перевода каждой «фразы» в нем на хинди для моей цели.
Я перешел в Power Shell к каталогу, где находится мой json-файл, и затем установил переменную среды, используя:
>>$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\Preeti\Downloads\Compressed\region_descriptions.json"
После этого я попытался открыть блокнот jupyter в том же каталоге и запустить код:
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 выдает мне ошибку, которая:
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 по ссылке, приведенной для лучшего понимания того, на какую «фразу» я ссылаюсь.