我正在努力学习塞尔维亚atm,并为自己准备了一个csv文件,其中包含了最常用的单词。
我现在想做的是让我的脚本通过API将每个单词输入到Google Translate中,并将此翻译保存到同一个文件中。
由于我是一个完全的Python和JSON初学者,我对如何使用API中的JSON感到非常困惑。在
我怎样才能得到译文?在
from sys import argv
from apiclient.discovery import build
import csv
import json
script, filename = argv
serbian_words = []
# Open a CSV file with the serbian words in one column (one per row)
with open(filename, 'rb') as csvfile:
serbianreader = csv.reader(csvfile)
for row in serbianreader:
# Put all words in one single list
serbian_words.extend(row)
# send that list to google item by item to have it translated
def main():
service = build('translate', 'v2',
developerKey='xxx')
for word in serbian_words:
translation = service.translations().list(
source='sr',
target='de',
q = word
).execute()
print translation # Until here everything works totally fine.
if __name__ == '__main__':
main()
对我来说,终端打印的是这样的{u'translations': [{u'translatedText': u'allein'}]}
,其中“allein”是塞尔维亚语单词的德语翻译。在
我怎么才能到“艾琳”?我试图通过实现Python附带的json编码器和解码器来解决这个问题,但我无法解决。在
我希望在这方面有任何帮助,我将非常感激。在
您可以使用项访问来访问最里面的字符串:
或者您可以循环查看列出的所有翻译(这是一个列表):
^{pr2}$因为谷歌的翻译服务可以为一个给定的文本提供多个翻译。在
相关问题 更多 >
编程相关推荐