【发布时间】:2018-11-04 11:03:33
【问题描述】:
Python 版本 3.5.2
所以我有来自mitmproxy 的文件输出称为enter code here,我想使用以下方法读取它:
from shelljob import proc
g = proc.Group()
command = "tail -f -c +0 output_file | mitmdump -n -r - --set flow_detail=1 --showhost"
p = g.run (command)
def read_process():
while g.is_pending():
lines = g.readlines()
for proc, line in lines:
print ("data:" + str(line) + '\n\n')
返回
data:b'tail: invalid number of lines: \xe2\x80\x98r\xe2\x80\x99\n'
但如果我将命令更改为ping 127.0.0.1,例如:
from shelljob import proc
g = proc.Group()
command = "ping 127.0.0.1"
p = g.run (command)
def read_process():
while g.is_pending():
lines = g.readlines()
for proc, line in lines:
print ("data:" + str(line) + '\n\n')
它工作正常!返回:
data:b'64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.056 ms\n'
那么解决办法是什么?
【问题讨论】:
-
您是否尝试在脚本文件的顶部添加`# -- coding: utf-8 --`?
-
我已经尝试添加# -- coding: utf-8 --,但同样的错误@LISHITING
-
那么,你到底在问什么?为什么您的命令
tail -f -c +0 output_file | mitmdump -n -r - --set flow_detail=1 --showhost无法按预期工作以及如何解决?因为从我的角度来看,输出的编码对您来说似乎并不像问题所暗示的那样严重 -
@LISHITING 该声明只声明了源文件的编码。如果源文件全是 ASCII(如图所示),则没有区别。
标签: python unicode character-encoding