【问题标题】:how to use Python's finditer to highlight list of items with different colors如何使用 Python 的 finditer 突出显示具有不同颜色的项目列表
【发布时间】:2021-09-09 18:18:42
【问题描述】:
-bash-4.2$ python3 --version
Python 3.6.8

在网上找到这个程序来突出文本中的单词

#!/usr/bin/env python3
import re
class bcolors:
    OK = '\033[92m' #GREEN
    ACTIVE = '\033[92m' #GREEN
    WARNING = '\033[93m' #YELLOW
    FAIL = '\033[91m' #RED
    ostkcd = '\033[91m' #RED
    ERROR = '\033[91m' #RED

#COLOR = ['red', 'blue', 'orange', 'violet', 'green']
COLOR = [bcolors.OK, bcolors.FAIL,bcolors.WARNING,bcolors.ERROR,bcolors.ACTIVE]
text = """Graham says that Perl is cooler than Java and Python than Perl. In some circles, maybe. Graham uses the example of Slashdot, written in Perl. But what about Advogato, written in C? What about all of the cool P2P stuff being written in all three of the languages? Considering that Perl is older than Java, and was at one time the Next Big Language, I think you would have a hard time getting statistical evidence that programmers consider Perl "cooler" than Java, except perhaps by virtue of the fact that Java has spent a few years as the "industry standard" (and is thus uncool for the same reason that the Spice Girls are uncool) and Perl is still "underground" (and thus cool, for the same reason that ambient is cool). Python is even more "underground" than Perl (and thus cooler?). Maybe all Graham has demonstrated is that proximity to Lisp drives a language underground. Except that he's got the proximity to Lisp argument backwards too."""

regex = re.compile(r"(\blisp\b)|(\bpython\b)|(\bperl\b)|(\bjava\b)|(\bc\b)", re.I)

i = 0; output = " "
for m in regex.finditer(text):
    output += "".join([text[i:m.start()],
                       "%s" % COLOR[m.lastindex-1],
                       text[m.start():m.end()],
                       " "])
    i = m.end()
print ("".join([output, text[m.end():], " "]))

但它不单独打印高亮字。它突出显示第一个单词正则表达式出现到第二个正则表达式单词出现,依此类推。它突出了这一切  如何像这样仅突出显示正则表达式单词

【问题讨论】:

    标签: python regex


    【解决方案1】:

    添加 bcolors.ENDC 解决了这个问题

    [![#!/usr/bin/env python3
    import re
    class bcolors:
        OK = '\033\[92m' #GREEN
        ACTIVE = '\033\[92m' #GREEN
        WARNING = '\033\[93m' #YELLOW
        FAIL = '\033\[91m' #RED
        ostkcd = '\033\[91m' #RED
        ERROR = '\033\[91m' #RED
        ENDC = '\033\[0m'
    
    COLOR = \[bcolors.OK, bcolors.FAIL,bcolors.WARNING,bcolors.ERROR,bcolors.ACTIVE\]
    text = """Graham says that Perl is cooler than Java and Python than Perl. In some circles, maybe. Graham uses the example of Slashdot, written in Perl. But what about Advogato, written in C? What about all of the cool P2P stuff being written in all three of the languages? Considering that Perl is older than Java, and was at one time the Next Big Language, I think you would have a hard time getting statistical evidence that programmers consider Perl "cooler" than Java, except perhaps by virtue of the fact that Java has spent a few years as the "industry standard" (and is thus uncool for the same reason that the Spice Girls are uncool) and Perl is still "underground" (and thus cool, for the same reason that ambient is cool). Python is even more "underground" than Perl (and thus cooler?). Maybe all Graham has demonstrated is that proximity to Lisp drives a language underground. Except that he's got the proximity to Lisp argument backwards too."""
    
    regex = re.compile(r"(\blisp\b)|(\bpython\b)|(\bperl\b)|(\bjava\b)|(\bc\b)", re.I)
    
    i = 0; output = " "
    for m in regex.finditer(text):
    
        output += "".join(\[text\[i:m.start()\],
                           "%s" % COLOR\[m.lastindex-1\],
                           text\[m.start():m.end()\],bcolors.ENDC,
                           " "])
        i = m.end()
    print ("".join(\[output, text\[m.end():\], " "\]))][1]][1]
    

    【讨论】:

      【解决方案2】:

      作为替代方案,您可以使用带有语言名称和所需颜色的字典。然后,使用字典items() 对每种颜色进行迭代,并使用re.sub 将语言替换为各自的颜色,同时保留从正则表达式组(\1)获取的原始文本。

      import re
      
      text = """Graham says that Perl is cooler than Java and Python than Perl. In some circles, maybe. Graham uses the example of Slashdot, written in Perl. But what about Advogato, written in C? What about all of the cool P2P stuff being written in all three of the languages? Considering that Perl is older than Java, and was at one time the Next Big Language, I think you would have a hard time getting statistical evidence that programmers consider Perl "cooler" than Java, except perhaps by virtue of the fact that Java has spent a few years as the "industry standard" (and is thus uncool for the same reason that the Spice Girls are uncool) and Perl is still "underground" (and thus cool, for the same reason that ambient is cool). Python is even more "underground" than Perl (and thus cooler?). Maybe all Graham has demonstrated is that proximity to Lisp drives a language underground. Except that he's got the proximity to Lisp argument backwards too."""
      
      color_dict = {
          'lisp' : '\033[91m', #RED
          'python' : '\033[94m', #BLUE
          'perl' : '\033[93m', #YELLOW
          'java' : '\033[95m', #PINK
          'c' : '\033[92m', #GREEN
      }
      
      for k, v in color_dict.items():
          text = re.sub(rf'\b({k})\b', rf'{v}\1\033[0m', text, flags=re.I)
      
      print(text)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多