Skills and tools 03 - call Baidu translation API for Chinese and English translation

Sometimes we need to translate between Chinese and English in our work. If the number of words is small, we can use the translation software manually If there is a large amount of translation, writing a program to automatically translate will be a better choice

This article uses python to write a script, calls Baidu translation API for automatic translation, and reads each line of the text file in turn, After translation, output to the result file

Baidu translation API

When automatic translation is needed, Google translation is the first thought. After all, it is recognized as the most accurate translation platform, Find the script experiment on the Internet and call it with http request Google translation The program fills in the fields so that After obtaining the translated results, the test found that it was not feasible to grab the translated content and view the web page source code It should be Google that puts the results in other locations instead of the current page; Google also provides translation API s, but for a fee, Not for the time being

Then I found Baidu translation naturally, and its translation platform is here: Baidu translation open platform. Its advantage is that the translation of less than 2 million words per month is free, which exceeds the re charge. For occasional translation like me It is basically free to use for people under

Before use, you need to click on the home page to apply for access and register. It will give APPID and key. These things are later The official documents have detailed instructions and examples. I won't say much, but go directly to the code of my script


#/usr/bin/env python
import sys
import httplib
import md5
import urllib
import random
import json


def trans_line(line, fromLang, toLang):
    result = []

    # replace with your id and key
    appid = ''
    secretKey = ''

    httpClient = None
    myurl = '/api/trans/vip/translate'
    q = line
    salt = random.randint(32768, 65536)

    sign = appid+q+str(salt)+secretKey
    m1 =
    sign = m1.hexdigest()
    myurl = myurl+'?appid='+appid+'&q='+urllib.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
        httpClient = httplib.HTTPConnection('')
        httpClient.request('GET', myurl)
        #response is an HTTPResponse object
        response = httpClient.getresponse()
        jsonData = json.loads(
        # print jsonData
    except Exception, e:
        print e
        if httpClient:

    return result

if __name__ == "__main__":
    fromLang = 'en'
    toLang = 'zh'

    if len(sys.argv) != 2:
        print "Enter a file name: "

    filename = sys.argv[1]
    tmp = filename.split(".")
    if len(tmp) != 2:
        print "Error filename"
    out_name = tmp[0] + '_en2zh.' + tmp[1]

    translated_list = []
    with open(filename, 'r') as fh_in:
        for line in fh_in:
            line = line.strip()
            if not line: continue

            translated_list.append(trans_line(line, fromLang, toLang))

    with open(out_name, 'w') as fh_out:
        for line_result in translated_list:
            #fh_out.write(line_result[1].encode("GBK") + "\n")
            fh_out.write(line_result[1] + "\n")

Test text en Txt as follows, power related English

Active power P
Reactive power Q
Apparent power S
Power factoer

Command line input: python en.txt. If there is no output, the operation is successful

Open the newly generated en_en2zh.txt:

Active power P
 Reactive power Q
 Apparent power S
 Power factor

The translation is absolutely correct

Can be in Management console View details of characters used

Added by ssjskipp on Wed, 02 Mar 2022 12:21:10 +0200