Well, I am a warm hearted person, as a programmer, while my sister is a program that apes, but there are specializing in this sentence is not white, sister to do data analysis, obtained the probably more than 6000 classification of words, need a little bit of translation, probably morning time, has been around one thousand words translation, the efficiency is too slow. Both delay chat with me, but also easy because I chat with him to delay work, so in the end how to say also can not get my good ah, and then I studied, should I try to call Baidu translation or Youdao translation interface try? Then, I silently began to check the translation of the API, because the vocabulary is not much, so called baidu, of course, if a lot of words, Baidu will charge. Well… All right, code!

#python3.6.5 # coding= UTF-8 #Strong by 20180810 from hashlib import MD5 from urllib import request import random import xlrd import json def fanyi(mes): Appid = "# your appID secretKey =" # your key httpClient = None myURL = Q = 'http://api.fanyi.baidu.com/api/trans/vip/translate' # API interface mes. Replace (" ", ""). The replace (" &", Print (q) # fromLang = 'en' # toLang = 'en' #中文 salt = random. Randint (32768, # generate random number sign = (appID + q + STR (salt) + secretKey) # key m1 = md5() # create hash5 m1.update(sign.encode(" utF-8 ")) sign = M1. hexDigest () # Generate an encrypted string myurL2 = myURL + '? Appid =' + AppID + '&q=' + q + '&from=' + fromLang + '&to=' + toLang + '&salt=' + STR (salt) + '&sign=' + sign # print(myurl2.replace(" ","")) req = request.Request(myurl2)# try: S = request. Urlopen (req) # except Exception as e: print(e) return {" SRC ": mes, "DST ": Mes} m = s.read().decode(" utF-8 ") js = eval(m) # print(js) trans_result = js["trans_result"] # STR (trans_result).strip("[").strip("]") # eval(l) # def read(): File_path = r 'i :\1.xls' # Read file path # read file path # file_path = file_path.decode('ANSI') # File in Chinese transcoding data = Table = data.sheet_by_name('Sheet1') # Obtain sheet nrows = table.nrows # Obtain total rows Er = open("D:/fanyicuowu.xls", "w") Print (" rows ", nrows) print(" rows ", ncols) for I in range(2, nrows, 1): for j in range(0, 4, 1): # print(" I ", "j",j) cell_value = table.cell(I,j). Value # print(cell_value) if cell_value == "": Pass # print(" KKKKKKK ") # break else: print(" I ", I,"j",j,cell_value) a = fanyi(cell_value) #翻译 try: F.write (a[" SRC "] + "\t") # write file f.write(a[" DST "] + "\n") # write file except Exception as u: Print (u) er.write(u + "\t") # write error er.close() f.close() return None if __name__ == '__main__': read()Copy the code

It took him about two hours to do what would have taken him about two days to do.

Although there is the possibility of not teasing, but they also learned ah, is not, knock code is the most important, as a programmer, how can not knock code? So, it’s a motivator, you never know when a girl gets hit on.