Python2 to Python3 convertion? TypeError: memoryview: a bytes-like object is required, not 'str'
Python2 to Python3 convertion? TypeError: memoryview: a bytes-like object is required, not '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)
我找到了一个代码:
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)