Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何操作以下文本_Python_Regex_Perl_Sed_Awk - Fatal编程技术网

Python 如何操作以下文本

Python 如何操作以下文本,python,regex,perl,sed,awk,Python,Regex,Perl,Sed,Awk,我想知道如何转换类似以下内容的文本: Chapter 3 Convex Functions 97 3.1 Definitions 98 3.2 Basic Properties 103 致: 通过使用一些方便但功能强大的文本操作语言和/或实用程序,如sed、awk、regex、perl、python 谢谢和问候 注: 在每一行中,重复最后一个数字。在Python中 "Chapter 3 Convex Functions 97".rsplit(None,1) 给予 使用文本块 txt = "

我想知道如何转换类似以下内容的文本:

Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103
致:

通过使用一些方便但功能强大的文本操作语言和/或实用程序,如sed、awk、regex、perl、python

谢谢和问候


注: 在每一行中,重复最后一个数字。

在Python中

"Chapter 3 Convex Functions 97".rsplit(None,1)
给予

使用文本块

txt = """Chapter 3 Convex Functions 97
    3.1 Definitions 98
    3.2 Basic Properties 103"""

for line in txt.split('\n'):
    line = line.strip().rsplit(None,1)
    print('("{0} {1}" "#{1}")'.format(*line))
给予

编辑:我已根据您的注释对其进行了更新,以便页码重复

import re
def format(str):
  m = re.search('(.*)\s(\d+)$', str)
  return "(\"" + m.group(1) + "\" \"#" +  m.group(2) + "\")"

print format('Chapter 3 Convex Functions 97')

print format('3.1 Definitions 98')

print format('3.2 Basic Properties 103')
返回

("Chapter 3 Convex Functions" "#97")
("3.1 Definitions" "#98")
("3.2 Basic Properties" "#103")

下面是一个Perl解决方案:

while (<DATA>) {
    s/^(.+ (\d+))$/("$1" "#$2")/;
    print;
}

__DATA__
Chapter 3 Convex Functions 97
3.1 Definitions 98
3.2 Basic Properties 103
或作为一个班轮:

perl -pe 's/^(.+ (\d+))$/("$1" "#$2")/'

适用于几乎所有版本的python

infile = open("input.txt")
outfile = open("output.txt", "w")

for line in infile:
    line, last = line.rstrip().rsplit(" ", 1)
    outfile.write('("%s %s" "#%s")\n' % (line, last, last))
结果

Chapter 3 Convex Functions 97 
3.1 Definitions 98  
3.2 Basic Properties 103

"Chapter 3 Convex Functions 97" "#97"
"3.1 Definitions 98" "#98"
"3.2 Basic Properties 103" "#103"

使用
sed
,有几种方法:

sed 's/\(.* \)\(.*\)/("\1\2" "#\2")/' inputfile

下面是一对使用AWK的夫妇:

awk '{n = $NF; print "(\"" $0 "\" \"#" n "\")"}' inputfile


在Python中-还有什么?这个答案有什么问题?没什么问题,只是澄清一下。提问者要求回答“sed,awk,regex,perl,python,…”这个答案没有提到所使用的语言。谢谢!注意:在每一行中,最后一个数字是重复的。我建议将
import re
移出您的函数体;还要预编译正则表达式。谢谢!注意:在每一行中,最后一个数字重复。谢谢!注意:在每一行中,最后一个数字重复。谢谢!我不熟悉Perl。对于单行程序解决方案,在点击return之后,它似乎希望我粘贴原始文本作为输入,然后我不知道如何告诉它我已经完成了输入。@Tim=>使用shell重定向会更容易:
perl-pe'…'output_file.txt
@nightcracker:谢谢!如何输出到另一个文件?@Tim:我会根据您的意愿编辑代码段,只要告诉我您希望在终端上输入什么。@nightcracker:我希望从一个文件输入并输出到另一个文件。谢谢@蒂姆:好了。编辑文件名,另存为somename.py,并使用
python somename.py
@nightcracker:谢谢!好奇的是,如何修改代码以去掉“last”末尾的换行符,从而使输出和输入是行到行的对应,而不是输入中的一行对应于输出中的三行?
infile = open("input.txt")
outfile = open("output.txt", "w")

for line in infile:
    line, last = line.rstrip().rsplit(" ", 1)
    outfile.write('("%s %s" "#%s")\n' % (line, last, last))
def munge(line):
    number = line.rsplit(None,1)[1]
    return '''("{0}" "#{1}")'''.format(line, number)
import re
pat = re.compile('^(.+?(\d+)) *$',re.M)

ch = '''Chapter 3 Convex Functions 97 
3.1 Definitions 98  
3.2 Basic Properties 103'''

print ch
print
print pat.sub('"\\1" "#\\2"',ch)
Chapter 3 Convex Functions 97 
3.1 Definitions 98  
3.2 Basic Properties 103

"Chapter 3 Convex Functions 97" "#97"
"3.1 Definitions 98" "#98"
"3.2 Basic Properties 103" "#103"
sed 's/\(.* \)\(.*\)/("\1\2" "#\2")/' inputfile
sed 's/\(.* \)\([0-9]*\)/("\1\2" "#\2")/' inputfile
awk '{n = $NF; print "(\"" $0 "\" \"#" n "\")"}' inputfile
awk 'BEGIN {q="\x22"} {n = $NF; print "(" q $0 q " " q "#" n q ")"}' inputfile