Python导入精确的hexa值并显示(Python Importing exact hexa value and display)
我有数据结构,我在其中定义了列表。
data_items = [ {"ID":0x0202, "Name":"Country"}, { "ID":0x2007, "Name":"Sports"}]我想导入完整的'ID'值,包括'0',例如:
'0202' '2007'现在打印如下:
'202' '2007'码:
Input = 1 SMI = data_items[Input-1]['ID'] hex(SMI)[2:]建议我怎么办?
I have the Data structure where I defined the list.
data_items = [ {"ID":0x0202, "Name":"Country"}, { "ID":0x2007, "Name":"Sports"}]I want to import complete 'ID' value including '0', like example:
'0202' '2007'Now it is printing as below:
'202' '2007'Code:
Input = 1 SMI = data_items[Input-1]['ID'] hex(SMI)[2:]Suggest me how can I proceed?
最满意答案
hex删除任何前导零。 如果要在字符串中保持4个字符的长度,请尝试使用hex(SMI)[2:].zfill(4) ,它将在前导零处,直到达到指定的长度。
hex removes any leading zeros. If you want to maintain a length of 4 characters in the string, try doing hex(SMI)[2:].zfill(4) where it will ad leading zeros until it reaches the specified length.
更多推荐
发布评论