第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將字節(jié)轉換為字符串?

將字節(jié)轉換為字符串?

紅糖糍粑 2019-06-03 09:55:53
將字節(jié)轉換為字符串?我使用這段代碼從外部程序獲得標準輸出:>>> from subprocess import *>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]Communications()方法返回一個字節(jié)數(shù)組:>>> command_stdoutb'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'但是,我希望將輸出作為一個普通的Python字符串來處理。這樣我就可以像這樣打印出來:>>> print(command_stdout)-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2我以為這就是b2a_qp()方法,但當我嘗試時,再次得到了相同的字節(jié)數(shù)組:>>> binascii.b2a_qp(command_stdout)b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'有人知道如何將字節(jié)值轉換回字符串嗎?我是說,用“電池”而不是手工操作。我希望Python 3沒有問題。
查看完整描述

4 回答

?
POPMUISE

TA貢獻1765條經(jīng)驗 獲得超5個贊

您需要解碼字節(jié)對象以生成字符串:


>>> b"abcde"

b'abcde'


# utf-8 is used here because it is a very common encoding, but you

# need to use the encoding your data is actually in.

>>> b"abcde".decode("utf-8") 

'abcde'


查看完整回答
反對 回復 2019-06-03
?
陪伴而非守候

TA貢獻1757條經(jīng)驗 獲得超8個贊

我認為這種方法很簡單:


bytes = [112, 52, 52]

"".join(map(chr, bytes))

>> p44


查看完整回答
反對 回復 2019-06-03
?
慕工程0101907

TA貢獻1887條經(jīng)驗 獲得超5個贊

您需要對字節(jié)字符串進行解碼,并將其轉換為字符(Unicode)字符串。

b'hello'.decode(encoding)

或者在Python 3上

str(b'hello', encoding)


查看完整回答
反對 回復 2019-06-03
  • 4 回答
  • 0 關注
  • 687 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號