【问题标题】:How to manipulate the following text如何操作以下文本
【发布时间】:2011-07-15 11:23:57
【问题描述】:

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

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、awk、regex、perl、python、...

感谢和问候!


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

【问题讨论】:

    标签: python regex perl sed awk


    【解决方案1】:

    在 Python 中,

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

    给予

    ['Chapter 3 Convex Functions', '97']
    

    处理一段文本,

    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))
    

    给予

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

    编辑:我已根据您的注释对其进行了更新,因此页码是重复的。

    【讨论】:

    • 在 Python 中 - 怎么回事?这个答案有什么问题?
    • 没什么问题,只是澄清一下。提问者用“sed, awk, regex, perl, python, ...”询问答案,而这个答案没有提到使用的语言。
    • 谢谢!注意:在每一行中,最后一个数字是重复的。
    【解决方案2】:
    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")
    

    【讨论】:

    • 我建议将import re 移出你的函数体;还预编译你的正则表达式。
    • 谢谢!注意:在每一行中,最后一个数字重复。
    【解决方案3】:

    这是一个 Perl 解决方案:

    while (<DATA>) {
        s/^(.+ (\d+))$/("$1" "#$2")/;
        print;
    }
    
    __DATA__
    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")
    

    或作为一个班轮:

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

    【讨论】:

    • 谢谢!注意:在每一行中,最后一个数字是重复的。
    • 谢谢!我不熟悉 Perl。对于one liner的解决方案,在hit return后,似乎期望我将原文粘贴为输入,然后我不知道如何告诉它我已经完成了输入。
    • @Tim => 使用 shell 重定向会更容易:perl -pe '...' &lt; input_file.txt &gt; output_file.txt
    【解决方案4】:

    适用于几乎所有版本的 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))
    

    【讨论】:

    • @nightcracker:谢谢!如何输出到另一个文件?
    • @Tim:我会根据你的需要编辑 sn-p,告诉我你想在终端上输入什么。
    • @nightcracker:我想从一个文件输入并输出到另一个文件。谢谢!
    • @Tim:完成。编辑文件名,保存为 somename.py 并使用python somename.py 执行。
    • @nightcracker:谢谢!只是好奇,如何修改代码去掉“last”末尾的换行符,让输出和输入是逐行对应,而不是输入中的一行对应输出中的三行?
    【解决方案5】:
    def munge(line):
        number = line.rsplit(None,1)[1]
        return '''("{0}" "#{1}")'''.format(line, number)
    

    【讨论】:

      【解决方案6】:
      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"
      

      【讨论】:

        【解决方案7】:

        这里有几种使用sed的方法:

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

        sed 's/\(.* \)\([0-9]*\)/("\1\2" "#\2")/' inputfile
        

        这里有一对使用 AWK:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-25
          • 2013-04-18
          • 2022-01-03
          • 1970-01-01
          • 2010-10-17
          相关资源
          最近更新 更多