[英]Get word definition/s from google translator using Python
才能滿足您的要求。 一種方法是在腳本中執行以下操作:
我制作了這個讀取 CSV 文件(我的機器上沒有安裝 excel)的簡單腳本,翻譯text
列下的所有內容並將它們放入已translated
的列。 如果您以不同方式處理數據,則取決於您。
注意下面的腳本:
pd.read_excel()
讀取excel個文件樣本數據.csv:
文本 | 虛擬字段 |
---|---|
跑步 | 假人1 |
你好嗎 | 假人2 |
跳 | 假人3 |
示例腳本:
import pandas as pd
from google.cloud import translate_v2 as translate
def translate_text(text):
translate_client = translate.Client()
target = 'tl'
result = translate_client.translate(text, target_language = target)
return result["translatedText"]
def process_data(input_file):
#df = pd.read_excel('test.xlsx', engine='openpyxl')
df = pd.read_csv(input_file)
df['translated'] = df['text'].apply(translate_text)
# move column 'translated' to second column
# this position will depend on your actual data
second_col = df.pop('translated')
df.insert(1, 'translated', second_col)
print(df)
df.to_csv('./updated_data.csv',index=False)
df.to_excel('./updated_data.xlsx',index=False)
process_data('sample_data.csv')
Output:
Dataframe
生成csv文件:
生成excel文件:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.