Python导入精确的hexa值并显示(Python Importing exact hexa value and display)

编程入门 行业动态 更新时间:2024-10-26 20:26:46
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.

更多推荐

本文发布于:2023-07-20 07:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1195965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:精确   hexa   Python   display   exact

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!