在python2中:
$ python2 -c 'print "\x08\x04\x87\x18"' | hexdump -C
00000000 08 04 87 18 0a |.....|
00000005
Run Code Online (Sandbox Code Playgroud)
在python3中:
$ python3 -c 'print("\x08\x04\x87\x18")' | hexdump -C
00000000 08 04 c2 87 18 0a |......|
00000006
Run Code Online (Sandbox Code Playgroud)
为什么"\xc2"这里有字节?
编辑:
我认为当字符串具有非ascii字符时,python3会将字节附加"\xc2"到字符串.(正如@Ashraful Islam所说)
那我怎么能在python3中避免这种情况呢?
我尝试过命令替换,但用空格替换'\n'
$ export GREENIE="$(python -c 'print "\n\r"*4')"
$ echo $GREENIE |hexdump
0000000 200d 200d 200d 0a0d
0000008
$
Run Code Online (Sandbox Code Playgroud)
我只需要这样:
$ echo $GREENIE |hexdump
00000000 0a0d 0a0d 0a0d 0a0d
00000008
$
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?