【问题标题】:How to do a double translation of a large text file using Python?如何使用 Python 对大型文本文件进行双重翻译?
【发布时间】:2019-03-23 08:08:45
【问题描述】:

我有一个大小为 2 x 400000 的 text.csv 文件,如下所示:

         col 1         col2
0        text          text
1        text          text
2        text          text
...
399999   text          text
400000   text          text

每列都需要先从英文翻译成法文,然后再翻译回英文。我尝试使用 Google 翻译手动执行此操作,但我的文件大小为 60MB,而 Google 翻译仅支持最大 1 MB 的文件。

这个 Eng > Fr > Eng 翻译可以使用 Python 自动完成吗?

【问题讨论】:

    标签: python-3.x google-translate


    【解决方案1】:

    您可以尝试以下方法来读取 csv 文件,并且文件中的每一行的两列都从英文翻译成法文,然后又翻译回英文:

    import csv
    from google.cloud import translate
    translate_client = translate.Client()
    def translateFunction(text,target):
            translation = translate_client.translate(text,target_language=target)
            return (translation['translatedText'])
    output_file = open('output.csv', 'wb')
    reader = csv.reader(open('source.csv', 'rU'), dialect=csv.excel_tab,delimiter=',')
    for row in reader:
        column1=translateFunction(translateFunction(row[1],'fr'),'en')
        column2=translateFunction(translateFunction(row[2],'fr'),'en')
        output_text = ','.join([row[0],column1,column2])
        output_file.write(output_text.encode('utf-8')+'\n')
    output_file.close()
    

    请记住,这会向 Translation API 发出多个请求。

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 2021-10-05
      • 2019-07-11
      • 2021-01-08
      • 2021-09-30
      • 2021-01-13
      • 2020-09-08
      • 2014-02-28
      相关资源
      最近更新 更多