我找到了一个代码:
https://github.com/rtulke/rpen/blob/master/rpen.py
它在Python2下工作得很好,但是当在Python3下运行时,有一个我无法修复的错误。你们谁能帮我一下?
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import sys
from optparse import OptionParser
from subprocess import Popen, PIPE, STDOUT
parser = OptionParser("usage: cat logfile | %prog [options] searchterm1 searchterm2...")
parser.add_option("-i", action="store_true", dest="ignore_case", default=False, help="perform a case insensitive search")
parser.add_option("-k", action="store_true", dest="display_all", default=False, help="only highlight, do not filter")
(options, args) = parser.parse_args()
colors = [
    ('green','04;01;32'),
    ('yellow','04;01;33'),
    ('red','04;01;31'),
    ('blue','04;01;34'),
    ('purple','0;04;35'),
    ('magenta','04;01;35'),
    ('cyan','04;01;36'),
    ('brown','0;04;33'),
    ]
if len(args) == 0:
    parser.print_help()
    sys.exit()
op = sys.stdin.read()
if not options.display_all:
    if options.ignore_case:
        p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
    else:
        p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy())
    op = p.communicate(input=op)[0]
for i,srch in enumerate(args):
    color = colors[i%len(colors)][1]
    env=os.environ.copy()
    env['GREP_COLORS'] = "mt="+color
    if options.ignore_case:
        p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)
    else:
        p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env)
    op = p.communicate(input=op)[0]
print(op)如果我更改为/usr/bin/env python3,则会出现以下python3错误。
Traceback (most recent call last):
  File "/usr/local/bin/rpen", line 45, in <module>
    op = p.communicate(input=op)[0]
  File "/usr/lib/python3.7/subprocess.py", line 939, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/usr/lib/python3.7/subprocess.py", line 1666, in _communicate
    input_view = memoryview(self._input)
TypeError: memoryview: a bytes-like object is required, not 'str'发布于 2021-09-01 13:32:52
设置编码可能会解决这个问题
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import sys
from optparse import OptionParser
from subprocess import Popen, PIPE, STDOUT
parser = OptionParser("usage: cat logfile | %prog [options] searchterm1 searchterm2...")
parser.add_option("-i", action="store_true", dest="ignore_case", default=False, help="perform a case insensitive search")
parser.add_option("-k", action="store_true", dest="display_all", default=False, help="only highlight, do not filter")
(options, args) = parser.parse_args()
colors = [
    ('green','04;01;32'),
    ('yellow','04;01;33'),
    ('red','04;01;31'),
    ('blue','04;01;34'),
    ('purple','0;04;35'),
    ('magenta','04;01;35'),
    ('cyan','04;01;36'),
    ('brown','0;04;33'),
    ]
if len(args) == 0:
    parser.print_help()
    sys.exit()
op = sys.stdin.read()
if not options.display_all:
    if options.ignore_case:
        p = Popen(["egrep", "|".join(args), "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
    else:
        p = Popen(["egrep", "|".join(args), "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=os.environ.copy(),encoding="utf-8")
    op = p.communicate(input=op)[0]
for i,srch in enumerate(args):
    color = colors[i%len(colors)][1]
    env=os.environ.copy()
    env['GREP_COLORS'] = "mt="+color
    if options.ignore_case:
        p = Popen(["egrep", srch+"|", "--color=always", "-i"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
    else:
        p = Popen(["egrep", srch+"|", "--color=always"], stdout=PIPE, stdin=PIPE, stderr=STDOUT, env=env,encoding="utf-8")
    op = p.communicate(input=op)[0]
print(op)https://stackoverflow.com/questions/69013950
复制相似问题