最初我有python 2.7代码,它可以读取文件并将其传递给perl代码:
proc = subprocess.Popen(["perl", _conlleval], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdout, _ = proc.communicate(open(filename).read())
现在我想将其转换为Python3代码,但在Python3 proc.communicate
中读取类似字节的对象而不是' str'。所以我使用stdout, _ = proc.communicate(open(filename).read().encode())
,但事实证明,将文件中的所有换行都转换为\n
,例如,在我有的文件中
Apple
Tree
Is
Good
但不幸的是编码会成功:
Apple\nTree\n\nIs\nGood\n
我该如何更正此错误?我想将字节格式的东西发送到珍珠而没有那些\n
答案 0 :(得分:0)
使用repr
:它返回一个包含对象可打印表示的字符串。
stri='''Apple
Tree
Is
Good
'''
print(stri)
print(repr(stri))
输出:
Apple
Tree
Is
Good
'Apple\nTree\n\nIs \nGood\n'