Preface: The first two articles reproduced other people's wonderful article, I also summarize it!
Recently started to use the PY, why?
I want to do a text similarity matching program, the general idea is two documents, one is the question, one is the material, I will test the question according to each problem, and then the content of each topic and the material to match the text similarity.
So the first thing to do is to cut each question as a string and store it in a dictionary.
Program into the following:
#-*-coding:utf-8-*-ImportRe#Regular Modulesf = open ('Test.txt','R') s=f.read () S1= S.split ('Staff Exams'#一级切割, separate each set of questionsPrintLen (S1) R1= Re.split ('(\d{1,3}\.)', s1[0]) #用正则按比如 "1.", "11.", "170." Cut the acres in each set of questions forIinchR1:Printif.close ()#Close File
Text content:
First, the problem (correct selection of a, the wrong choice B, the corresponding position on the answer card: 1 points per question, total 20)1. Communication is only the transfer of meaning between two or more two people. ()2. The control span of flat type organization is smaller. ()3. The quality of enterprise personnel is generally high, the intention of leadership can be well understood, it is appropriate to take centralized management. ()4. The key to decision is the goal, there is no decision without the goal. ()5. Brainstorming is developed to ensure the creativity of group decision-making, improve decision quality and improve group decision-making. ()6. A good leader not only relies on his legal status, but also can motivate others to work with personal talent, boldness and prestige. ()
Output Result:
Python 2.7.10 (default, May, 09:40:32) [MSC v.1500 32bit (Intel)] on Win32type"Copyright","credits" or "license ()" forMore information.>>> ================================ RESTART ================================>>> 13April 16, 2016 Chongqing Qijiang District public institutions open recruitment One, the problem (correct selection of a, the wrong choice B, applied to the corresponding location of the answer card: 1 points for each question, a total of 20)1. Communication is only the transfer of meaning between two or more two people. ()2. The control span of flat type organization is smaller. ()3. The quality of enterprise personnel is generally high, the intention of leadership can be well understood, it is appropriate to take centralized management. ()4. The key to decision is the goal, there is no decision without the goal. ()5. Brainstorming is developed to ensure the creativity of group decision-making, improve decision quality and improve group decision-making. ()6. A good leader not only relies on his legal status, but also can motivate others to work with personal talent, boldness and prestige. ()
The final explanation:
Re.split ('(\d{1,3}\.) ', s1[0]) #这里若去掉括号, the split symbol is not preserved
The output results are as follows:
>>> ================================ RESTART ================================>>> April 16, 2016 Chongqing Qijiang District public institutions open recruitment One, the judgment problem (correct choice A, the wrong choice B, the corresponding position on the answer card: 1 points per question, a total of 20) communication is only two or more than two people of the meaning of transmission. () the control span of the flat type organization is smaller. () The quality of the enterprise is generally high, the intention of the leadership can be well understood, it is appropriate to take centralized management. the key to decision-making is the goal, and there is no decision without a goal. () brainstorming is developed to ensure the creativity of group decision-making, improve decision-making quality and improve group decision-making. () A good leader not only relies on the authority conferred on him by a lawful position, but also is able to motivate others to work with personal talent, boldness and prestige. ()
The first two articles reproduced other people's wonderful article, I also summarize the use of Python split it!