我試圖解決這個問題一段時間。已經找到了簡單的答案,但無濟於事。任何幫助非常感謝。我創建了一個python字典,並試圖將值的輸出格式化爲只有二進制數據。換句話說,對於字典中的每個字符串值,我想輸出它的二進制值。我的代碼和我得到的錯誤如下。輸出Python字典'值'爲二進制格式
pigpen = {}
pigpen['a'] = 'ETL'
pigpen['b'] = 'ETM'
pigpen['c'] = 'ETR'
pigpen['d'] = 'EML'
pigpen['e'] = 'EMM'
pigpen['f'] = 'EMR'
pigpen['g'] = 'EBL'
pigpen['h'] = 'EBM'
pigpen['i'] = 'EBR'
pigpen['j'] = 'DTL'
pigpen['k'] = 'DTM'
pigpen['l'] = 'DTR'
pigpen['m'] = 'DML'
pigpen['n'] = 'DMM'
pigpen['o'] = 'DMR'
pigpen['p'] = 'DBL'
pigpen['q'] = 'DBM'
pigpen['r'] = 'DBR'
pigpen['s'] = 'EXT'
pigpen['t'] = 'EXL'
pigpen['u'] = 'EXR'
pigpen['v'] = 'EXB'
pigpen['w'] = 'DXT'
pigpen['x'] = 'DXL'
pigpen['y'] = 'DXR'
pigpen['z'] = 'DXB'
import binascii
str = pigpen.values()
print ' '.join(format(ord(string), 'b') for string in str)
Traceback (most recent call last):
File "pigpen_build.py", line 62, in <module>
print ' '.join(format(ord(string), 'b') for string in str)
File "pigpen_build.py", line 62, in <genexpr>
print ' '.join(format(ord(string), 'b') for string in str)
TypeError: ord() expected a character, but string of length 3 found
>>>
你是什麼意思的二進制值?你能提供一個示例輸出嗎? – amirouche
二進制值是該角色的ascii值。看看python的'ord'和'chr'函數。 –
非常感謝幫助傢伙。 – Dac121