Python2 到 Python3 的转换?类型错误:内存视图:需要类似字节的对象,而不是“str”

克雷

我找到了一个代码:

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'
拉帕

设置编码可能应该修复它

#!/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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章