首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python regex txt

python regex txt
EN

Stack Overflow用户
提问于 2016-12-14 12:33:41
回答 2查看 62关注 0票数 2

我有一个1.txt:

代码语言:javascript
复制
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

我想得到这样的结果:

代码语言:javascript
复制
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
...

两个变化:

代码语言:javascript
复制
1. I I.1 use the TAB.
2. all number in the end plus 0.1

我试着用熊猫,但这不是工作。我试着用别的方法,但我不知道怎么写下一个程序:

代码语言:javascript
复制
# -*- 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

该方案的结果:

代码语言:javascript
复制
['1.1', '1.1', '13.1', '19.1', '19.1', '41.1', '51.1']
['\tI.1', '\tI.2', '\tII.1', '\tII.2', '\tII.3']
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-14 12:57:08

你可以试试这个:

代码语言:javascript
复制
(.*)(\d+)

并以下列案文取代:

代码语言:javascript
复制
\1\2.1

解释

样本代码:

代码语言:javascript
复制
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)

样本输出:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2016-12-14 14:09:54

对于TAB缩进,可以使用以下代码:

代码语言:javascript
复制
for m in re.finditer(r'[IVX]+\.\d.*\n',result):
  oldgroup = m.group()
  newgroup = '\t' + oldgroup
  result = re.sub(oldgroup,newgroup,result)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41142797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档