我有一个大小为2x400000的text.csv文件,如下所示:
col 1 col2
0 text text
1 text text
2 text text
...
399999 text text
400000 text text每一栏都需要从英文翻译成法文,然后再翻译成英文。我试着用Google翻译手工完成这个操作,但是我的文件大小是60 MB,Google翻译只支持1MB以下的文件。
这个Eng > Fr > Eng翻译可以用Python自动完成吗?
发布于 2018-10-18 20:13:41
您可以尝试以下方法来读取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执行几个请求。
https://stackoverflow.com/questions/52875317
复制相似问题