pthon如何将英文翻译为中文
时间: 2024-05-16 17:13:39 浏览: 22
要将英文翻译为中文,可以使用 Python 中的翻译 API 或翻译库。以下是一些常用的方法:
1. 使用 Google 翻译 API:
```
pip install googletrans==4.0.0-rc1
from googletrans import Translator
translator = Translator()
text = "hello, world"
result = translator.translate(text, dest='zh-CN')
print(result.text)
```
2. 使用百度翻译 API:
```
pip install baidu_translate
from baidu_translate import BaiduTranslateApi
api = BaiduTranslateApi(appid='your_app_id', secretKey='your_secret_key')
text = "hello, world"
result = api.translate(text, 'en', 'zh')
print(result['trans_result'][0]['dst'])
```
3. 使用有道翻译 API:
```
pip install youdao-api
from youdaoapiv3 import YouDaoApiV3
api = YouDaoApiV3(app_key='your_app_key', app_secret='your_app_secret')
text = "hello, world"
result = api.translate(text, 'en', 'zh-CHS')
print(result['translation'][0]['tgt'])
```
以上是一些常用的 Python 翻译库和 API,你可以根据自己的需要选择适合的方式进行翻译。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)