您的位置:首页 > 编程语言 > Python开发

python3百度翻译API实现批量翻译

2017-10-29 23:52 309 查看
具体代码和示例如下,有个问题就是如果有一个空行会报错,还没弄懂出错的机制

import http
import hashlib
import urllib.request
import random
import json

while True:
fin = open(r'D:\1.txt', 'r')               #以读的方式打开输入文件
fout = open(r'D:\2.txt', 'w')             #以写的方式打开输出文件
for eachLine in fin:
appid = '自己注册'  # 参考百度翻译后台,申请appid和secretKey
secretKey = '同理'
httpClient = None
myurl = '/api/trans/vip/translate'
q = eachLine.strip()                   #文本文件中每一行作为一个翻译源
fromLang = 'zh'                         #中文
toLang = 'en'                             #英文
salt = random.randint(32768, 65536)
sign = appid+q+str(salt)+secretKey
sign = sign.encode('UTF-8')
m1 = hashlib.md5()
m1.update(sign)
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+urllib.parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
#response是HTTPResponse对象
response = httpClient.getresponse()
html= response.read().decode('UTF-8')
target2 = json.loads(html)
#print(target2.get("trans_result"))
src = target2["trans_result"][0]["dst"]
print(src)#取得翻译后的文本结果,测试可删除注释
outStr = src
fout.write(outStr.strip() + '\n')
fin.close()
fout.close()
print('翻译成功,请查看文件')
break


原文

新华社北京10月28日电  十九届中共中央政治局10月27日上午就深入学习贯彻党的十九大精神进行第一次集体学习。中共中央总书记习近平在主持学习时强调,党的十九大在政治上、理论上、实践上取得了一系列重大成果,就新时代坚持和发展中国特色社会主义的一系列重大理论和实践问题阐明了大政方针,就推进党和国家各方面工作制定了战略部署,是我们党在新时代开启新征程、续写新篇章的政治宣言和行动纲领。贯彻落实党的十九大精神,在新时代坚持和发展中国特色社会主义,要求全党来一个大学习。
  李克强、栗战书、汪洋、王沪宁、赵乐际、韩正就深刻领会和贯彻落实党的十九大精神谈了体会。他们表示,党的十九大高举中国特色社会主义伟大旗帜,以马克思列宁主义、毛泽东思想、邓小平理论、“三个代表”重要思想、科学发展观、习近平新时代中国特色社会主义思想为指导,作出了中国特色社会主义进入了新时代等重大政治论断,深刻阐述了新时代中国共产党的历史使命,确定了决胜全面建成小康社会、开启全面建设社会主义现代化国家新征程的目标,对新时代推进中国特色社会主义伟大事业和党的建设新的伟大工程作出了全面部署。党的十九大报告进一步指明了党和国家事业前进方向,是我们党团结带领全国各族人民在新时代坚持和发展中国特色社会主义的政治宣言和行动纲领,是马克思主义的纲领性文献。全党全国要紧密团结在以习近平同志为核心的党中央周围,增强“四个意识”,坚定“四个自信”,统筹推进“五位一体”总体布局,协调推进“四个全面”战略布局,坚持党要管党、全面从严治党,使党的十九大精神成为推动党和国家事业发展的强大思想武器,把党的十九大提出的各项目标任务落到实处。
  中共中央政治局各位同志认真听取了他们的意见。
  习近平在主持学习时发表了讲话。他指出,学习宣传贯彻党的十九大精神是全党全国当前和今后一个时期的首要政治任务。中央政治局要先学一步,为全党作出示范。在座不少同志是新进中央政治局的同志,位子更高了,担子更重了,站位就要更高,眼界就要更宽。大家要把学习贯彻党的十九大精神作为第一堂党课、第一堂政治必修课,努力提高自己的政治素养和思想理论水平,以更好担负起党和人民赋予的重要职责。
  习近平强调,学习贯彻党的十九大精神,要在学懂上下功夫。学懂是前提。党的十九大提出了许多新理念、新论断,确定了许多新任务、新举措,需要通过学习来准确领会。关键是要多思多想,努力掌握党的十九大精神的政治意义、历史意义、理论意义、实践意义。要注重采取理论和实践、历史和现实、当前和未来相结合的方法,把每一点都领会深、领会透。要坚持马克思主义立场观点方法,从我国实际出发,遵循我国发展的逻辑,增强中国特色社会主义道路自信、理论自信、制度自信、文化自信。
  习近平指出,学习贯彻党的十九大精神,要在弄通上下功夫。要联系地而不是孤立地、系统地而不是零散地、全部地而不是局部地理解党的十九大精神,不能就事论事,不能搞形式主义、实用主义。要把学习贯彻党的十九大精神同学习马克思主义基本原理贯通起来,把学习贯彻党的十九大精神同把握党的十八大以来我们进行伟大斗争、建设伟大工程、推进伟大事业、实现伟大梦想的实践贯通起来,把学习贯彻党的十九大精神同把握党的十九大作出的各项战略部署贯通起来,深化认识党的十九大关于党和国家事业各项战略部署的整体性、关联性、协同性,全面做好党和国家各项工作。
  习近平强调,学习贯彻党的十九大精神,要在做实上下功夫。清谈误国、实干兴邦,一分部署、九分落实。要拿出实实在在的举措,一个时间节点一个时间节点往前推进,以钉钉子精神全面抓好落实。中央政治局的同志要带好头,真抓实干,埋头苦干,把分管的工作抓紧抓实、抓出成效。党中央要统筹党的十九大提出的各项目标任务,就重大目标任务作出顶层设计和全面部署。全国人大、国务院、全国政协、中央军委等各有关部门和有关方面要自觉行动起来,明确属于自己职责范围内的任务,找准工作方案,排出任务表、时间表、路线图,对做好工作提出明确要求,重点是质量要求。党的十九大确定的目标任务有近期的,有中期的,也有长期的,要分清轻重缓急,有计划有秩序加以推进。各地区各部门要结合自身实际,把党中央提出的战略部署转化为本地区本部门的工作任务。要牢固树立全国一盘棋思想,以贯彻党中央决策部署为前提,确保党中央确定的目标任务和战略部署顺利实现。
  习近平指出,新征程上,不可能都是平坦的大道,我们将会面对许多重大挑战、重大风险、重大阻力、重大矛盾,领导干部必须有强烈的担当精神。领导干部不仅要有担当的宽肩膀,还得有成事的真本领。既要大胆讲政治,又要善于讲政治;既要矢志抓发展,又要善于抓发展;既要勇于抓改革,又要善于抓改革;既要敢于直面矛盾和问题,又要善于化解矛盾和问题;既要有想干事、真干事的自觉,又要有会干事、干成事的本领。
  习近平强调,当前,全党全国上下正在深入学习宣传贯彻党的十九大精神,开会发文是传达精神的必要方式,营造浓厚氛围也是必要的,但要防止出现以会议落实会议、以文件落实文件的现象,不能空喊口号、流于形式。各地区各部门要按照党中央统一部署,采取多种方式,运用多种载体,加大宣传力度,迅速兴起学习宣传贯彻热潮。各级党委要突出抓好县处级以上领导干部的学习,党委(党组)中心组要把学习党的十九大精神作为重中之重。各级党校、行政学院要把学习贯彻党的十九大精神作为干部培训的主要内容。各地区各部门要抓紧组织对干部进行集中轮训。要抓好面向广大群众的宣传教育,深入浅出向广大群众宣传解读好党的十九大精神。领导干部要做实干家,也要做宣传家。各级领导干部要带头宣讲,面向群众宣传党的十九大精神。还要组织党的十九大代表到基层宣讲,以自己的亲身经历、切身感受宣传党的十九大精神。


译文

Xinhua news agency, Beijing, October 28, October 27th, the nineteen CPC Central Committee Political Bureau on the morning of to further study and implement the spirit of the party's first collective learning. The CPC Central Committee General Secretary Xi Jinping presided ov
10887
er the study stressed that the nineteen party in political theory and practice made a series of major achievements, a series of major theoretical and practical issues of socialist development China characteristics of new era and adhere to clarify the fundamental policy, to promote the party and the country in all aspects of work to develop a strategic plan that is a political declaration and programme of action of our party in the new era opens a new journey, writing a new chapter. Carrying out the nineteen spirits of the party, adhering to and developing socialism with Chinese characteristics in the new era requires the whole party to have a great learning.
Li Keqiang, Wang Yang, Wang Huning, Zhao Leji, Li Zhanshu, Han Zheng deeply understand and implement the spirit of the party's nineteen big talk about experience. They said that the great banner of socialism with Chinese nineteen party, Marx Lenin, Mao Zedong thought, Deng Xiaoping's theory and the important thought of "Three Represents", Scientific Outlook on Development, Xi Jinping China new era of socialism as the guiding ideology of socialism, made China entered the new era of major political judgment, expounded the new era the history of the Communist Party of China mission, established a comprehensive well-off society and the comprehensive construction of a modern socialist country opens a new journey, made the comprehensive deployment of the new great project of building a new era to promote the great cause of socialism with Chinese characteristics and party. The nineteen Party Congress report further pointed out the cause of the party and the national direction, is our party to unite the political declaration and programme of action led the people of all ethnic groups in the new era of upholding and developing socialism Chinese, is a programmatic document of the Marx doctrine. The whole party should unite closely around the Party Central Committee with Comrade Xi Jinping as the core, strengthen the "Four Consciousness", to strengthen the four self-confidence, promote the overall development of "five in one overall layout, to promote the coordination of" four comprehensive "strategic layout, adhere to the party to manage the party strictly comprehensive, so that the spirit of the party's nineteen become a powerful ideological weapon to promote the development of the party and the state, the nineteen put forward the objectives and tasks of the party to implement.
Comrades of the Political Bureau of the Central Committee of the CPC listened to their opinions carefully.
Xi Jinping made a speech while presiding over his studies. He pointed out that learning, publicizing and carrying out the nineteen spirits of the party is the first political task of the whole Party and the whole nation at present and in the future. The Central Political Bureau should first learn a lesson and demonstrate it for the whole party. Many comrades here are comrades of the New Political Bureau of the Central Committee of the Communist Party of China. The seats are higher, the burden is heavier, the station will be higher, and the horizons will be wider. We should study and implement the spirit of the party's nineteen as the first dangke, first class of political courses, and strive to improve their political quality and ideological level, to better shoulder the important responsibilities entrusted by the party and the people.
Xi Jinping stressed that the study and implementation of the party's nineteen major spirit, we must learn to understand and work hard. Learning to understand is the premise. The party's nineteen big put forward many new ideas, new judgments, identified many new tasks, new initiatives, need to learn through accurate understanding. The key is to think more like, to grasp the spirit of the party's nineteen great political significance and historical significance, theoretical significance, practical significance. We should pay attention to the combination of theory and practice, history and reality, present and future, and grasp every point deeply and thoroughly. We should adhere to Marx's standpoint, viewpoint and method, proceed from the reality of our country, follow the logic of our country's development, and enhance the confidence, theoretical confidence, institutional confidence and cultural confidence of socialism with Chinese characteristics.
Xi Jinping pointed out that the study and implement the spirit of the party's nineteen, to be in a good grasp on Kung fu. To contact the ground rather than in isolation, systematic and not scattered, all not to understand the local spirit of the party's nineteen not factual, can not engage in formalism, pragmatism. To study and implement the spirit of the party's nineteen with the basic principle of the Marx doctrine through learning, to study and implement the spirit of the party's nineteen to grasp the party's eighteen big since we were great struggle, the construction of the great project, promote the great cause, realize the dream through great practice up to study and implement the spirit of the party's nineteen with the grasp of the party's nineteen strategic deployment to link up, deepen the understanding of the party's nineteen on the cause of the party and the state of the strategic deployment of the integrity, relevance, collaboration, complete all the work for the party and the state.
Xi Jinping stressed that the study and implement the spirit of the party's nineteen, to do work hard. Talk Wuguo, hard work and prosperous, one of the deployment, the implementation of nine points. To take tangible measures, a time node, a time node forward, to nail the spirit of fully implement. The Political Bureau of the Central Committee of the comrades should take the lead, work hard, work hard, put in charge of the work firmly and realistically achieve tangible results. The Central Committee of the Communist Party of China should make overall planning of the nineteen objectives and tasks of the party, and make top-level design and overall deployment of major objectives and tasks. The National People's Congress, the State Council, the CPPCC National Committee and the Central Military Commission and other relevant departments and the relevant aspects should consciously act to clear their own duties within the scope of the task, identify the programme of work, the task list, discharge timetable and roadmap, to make clear demands, focus on quality requirements. The nineteen major objectives and tasks of the party are short-term, mid-term and long-term, and should be prioritized and planned and orderly. All localities and departments should combine their own actual situation and transform the strategic deployment proposed by the Central Party committee into the task of the local departments in this region. Whole country to firmly establish the idea to implement the CPC Central Committee decision to deploy the premise, to ensure that the Party Central Committee to determine the objectives and tasks and strategic deployment implementation.
Xi Jinping pointed out that, on the new journey, it is impossible to be a smooth road, we will face many major challenges, major risks, major resistance, major contradictions, leading cadres must have a strong spirit of taking responsibility. Leading cadres should not only have the ability to play wide shoulders, really have nothing. To speak of politics, but also good at politics; not only to grasp the development, but also be good at grasping the development; both have the courage to reform, but also be good at grasping the reform; not only should have the courage to face the contradictions and problems, but also good at solving contradictions and problems; not only have the general really think general, consciously, and stewards, the ability to do nothing.
Xi Jinping stressed that at present, the spirit of the party and the country is on the in-depth study and implementation of the party's nineteen, the meeting issued a document is a necessary way to convey the spirit, to create a strong atmosphere is necessary, but to prevent to implement the conference meeting, to document the implementation of documents of the phenomenon, not empty slogans, a mere formality. All localities and departments should deploy in accordance with the Central Committee of the CPC, adopt various ways, use various kinds of carriers, intensify propaganda, and rapidly promote the upsurge of learning, propaganda and implementation. Party committees at all levels should give prominence to the study of leading cadres at the county level and above, and the Central Committee of the Party committee (Party group) should take the nineteen major spirits of learning the party as the most important. Party schools and administrative colleges at all levels should study and implement the nineteen major spirits of the party as the main contents of cadre training. All localities and departments should pay close attention to the organization of cadres concentrated rotation. We should do a good job of publicity and education for the broad masses of the people, understand the nineteen spirits of the party in a simple way and publicize it to the masses. Leading cadres should be doers and propagandists. Leading cadres at all levels should take the lead in preaching and promoting the party's nineteen spirits for the masses. We should also organize the nineteen representatives of the party to preach at the grassroots level, and publicize the nineteen spirits of the party with their own experience and personal experience.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: