python正则表达式
我有一个1.txt文件:python正则表达式,python,regex,Python,Regex,我有一个1.txt文件: I. Introduction to Text Mining 1 I.1 Defining Text Mining 1 I.2 General Architecture of Text Mining Systems 13 II. Core Text Mining Operations 19 II.1 Core Text Mining Operations 19 II.2 Using Background Knowledge for Text Mining 41 II.
I. Introduction to Text Mining 1
I.1 Defining Text Mining 1
I.2 General Architecture of Text Mining Systems 13
II. Core Text Mining Operations 19
II.1 Core Text Mining Operations 19
II.2 Using Background Knowledge for Text Mining 41
II.3 Text Mining Query Languages 51
III. Text Mining Preprocessing Techniques 57
III.1 Task-Oriented Approaches 58
III.2 Further Reading 62
IV. Categorization 64
IV.1 Applications of Text Categorization 65
IV.2 Definition of the Problem 66
IV.3 Document Representation 68
我想得到这样的结果:
I. Introduction to Text Mining 1.1
I.1 Defining Text Mining 1.1
I.2 General Architecture of Text Mining Systems 13.1
II. Core Text Mining Operations 19.1
II.1 Core Text Mining Operations 19.1
II.2 Using Background Knowledge for Text Mining 41.1
II.3 Text Mining Query Languages 51.1
...
两个变化:
1. I I.1 use the TAB.
2. all number in the end plus 0.1
我试着用熊猫,但没用。我尝试了其他方法,但我不知道如何编写下一个程序:
# -*- coding: utf-8 -*-
import re
f=open("D:/Downloads/1.txt")
page_list = []
content=[]
for line in f:
if re.search('(\d+)$',line) !=None:
page_list.append(re.search('(\d+)$',line).group())
if re.search('^(.*\.\d+)',line) !=None:
content.append(re.search('^(.*\.\d+)',line).group())
str=map(lambda x:x+'.1',page_list)
print str
con=map(lambda x:'\t'+x,content)
print con
该计划的结果是:
['1.1', '1.1', '13.1', '19.1', '19.1', '41.1', '51.1']
['\tI.1', '\tI.2', '\tII.1', '\tII.2', '\tII.3']
您可以尝试以下方法:
(.*)(\d+)
并替换为:
\1\2.1
示例代码:
import re
regex = r"(.*)(\d+)"
test_str = ("I. Introduction to Text Mining 1\n"
"I.1 Defining Text Mining 1\n"
"I.2 General Architecture of Text Mining Systems 13\n\n"
"II. Core Text Mining Operations 19\n"
"II.1 Core Text Mining Operations 19\n"
"II.2 Using Background Knowledge for Text Mining 41\n"
"II.3 Text Mining Query Languages 51\n\n"
"III. Text Mining Preprocessing Techniques 57\n"
"III.1 Task-Oriented Approaches 58\n"
"III.2 Further Reading 62\n\n"
"IV. Categorization 64\n"
"IV.1 Applications of Text Categorization 65\n"
"IV.2 Definition of the Problem 66\n"
"IV.3 Document Representation 68\n\n")
subst = "\\1\\2.1"
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
样本输出:
I. Introduction to Text Mining 1.1
I.1 Defining Text Mining 1.1
I.2 General Architecture of Text Mining Systems 13.1
II. Core Text Mining Operations 19.1
II.1 Core Text Mining Operations 19.1
II.2 Using Background Knowledge for Text Mining 41.1
II.3 Text Mining Query Languages 51.1
III. Text Mining Preprocessing Techniques 57.1
III.1 Task-Oriented Approaches 58.1
III.2 Further Reading 62.1
IV. Categorization 64.1
IV.1 Applications of Text Categorization 65.1
IV.2 Definition of the Problem 66.1
IV.3 Document Representation 68.1
对于
选项卡
缩进,您可以使用以下代码:
for m in re.finditer(r'[IVX]+\.\d.*\n',result):
oldgroup = m.group()
newgroup = '\t' + oldgroup
result = re.sub(oldgroup,newgroup,result)
如果你给我一些时间,我会为你编码。你能粘贴你的整个输入文件或给出你的罗马数字的上限吗?@Shiv是的。我已经更改了代码,第一个代码是1.txt。谢谢对于I.1 I.2 III.1 III.2…..IV.3plz开头的制表符缩进,我应该怎么做?请查看您的示例输出,如果需要更改,请对其进行修改