|
楼主 |
发表于 2021-7-28 14:32:32
|
显示全部楼层
读取CSV文件某一列,翻译到指定CSV文件
- import requests
- import pandas as pd
- import json
- pd1 = pd.read_csv('d:/fanyi.csv',encoding='gbk')
- neirong = ','.join(pd1['翻译'])
- liebiao = list(neirong.split(','))
- url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
- UA = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'}
- respon = requests.post( url = url,headers=UA).text
- for i in liebiao:
- data = {}
- data['i'] = i
- data['from'] = 'AUTO'
- data['to'] = 'AUTO'
- data['client'] = 'fanyideskweb'
- data['doctype'] = 'json'
- respon = requests.post( url = url,headers=UA,data=data).text
- zidian = json.loads(respon)
- zidian=zidian['translateResult'][0][0]['tgt']
- shuju = pd.DataFrame({'a':[i],'b':[zidian]})
- shuju.to_csv('d:/fanyi31.csv',index=False,header=0,mode='a',encoding='ANSI')
复制代码
|
|