Python如何以预期格式显示结果

时间:2017-04-13 10:38:34

标签: python

我使用python来操作数据并以预期的格式显示

目前我使用空数组根据数据类型(U8和U16)追加操纵数据

对于U8数据类型:Value = 255

我得到了结果:request = 2F f4 0d ['ff', 0, 0, 0]

我希望通过限制添加字节来显示结果:request = 2F f4 0d ff

对于U16数据类型:Value = 260

我得到了结果:request = 2F f4 0c ['04', '10', 0, 0]

我希望通过限制添加字节来显示结果:request = 2F f4 0d 04 10

代码如下:

def EventOnBtnIOControl(event):

#Called the Force Numeric Item to open GUI
ForceNumericItem()

global Value
global CMD

top.wait_window(Subtop)
request = '2F'+ ' '+'f4'+' '+'0d'+' '+ str(CMD)
print request
return

我的GUI功能:

def ForceNumericItem():
 Subtop.bind('<Return>', EventForceValue)
 return

点击Enter

def EventForceValue(event):
global Value
Value = SubMenuTakeStrInt()
print SMIScale
fScaledAndOffset = ( ( Value - SMIOffset ) / SMIScale )
print fScaledAndOffset

if (fScaledAndOffset >= 0.0):
    iScaledAndOffset = math.trunc(fScaledAndOffset + 0.5)
else:
    iScaledAndOffset = math.trunc(fScaledAndOffset - 0.5)
print iScaledAndOffset
global CMD

CMD = [0,0,0,0]

if(SMIType == 'u8' or SMIType == 's8'):
    CMD[0] = hex(iScaledAndOffset & 0xff)[2:].zfill(2)
    CMDlimit = 1
    print CMD

elif (SMIType == 'u16' or SMIType == 's16'):
    CMD[0] = hex(( iScaledAndOffset >> 8 ) & 0xff)[2:].zfill(2)
    CMD[1] = hex(iScaledAndOffset & 0xff)[2:].zfill(2)
    CMDlimit = 2
    print CMD
elif (SMIType == 'u24'):
    CMD[0] =  hex(( iScaledAndOffset >> 16 ) & 0xff)[2:].zfill(2)
    CMD[2] = hex(( iScaledAndOffset >> 8 ) & 0xff)[2:].zfill(2)
    CMD[3] = hex(iScaledAndOffset & 0xff)[2:].zfill(2)
    print CMD
elif (SMIType == 'u32'):
    CMD[0] =  hex(( iScaledAndOffset >> 24 ) & 0xff)[2:].zfill(2)
    CMD[1] =  hex(( iScaledAndOffset >> 16 ) & 0xff)[2:].zfill(2)
    CMD[2] = hex(( iScaledAndOffset >> 8 ) & 0xff)[2:].zfill(2)
    CMD[3] = hex(iScaledAndOffset & 0xff)[2:].zfill(2)
    print CMD
else:
    printf(  "Invalid Numeric Data by ID Type", Red )
Subtop.destroy()
top.deiconify()

return

1 个答案:

答案 0 :(得分:0)

您的#!/bin/bash for day in "MO" "TU" "WE" "TH" "FR" "SA" "SU" do mapfile $day < <(grep "$day" days | awk '{ printf $1"\n" }') done

CMD

您正在使用

显示它
CMD = [0,0,0,0]

可能request = '2F'+ ' '+'f4'+' '+'0d'+' '+ str(CMD) print request 是您问题的根源,因为它会将列表转换为列表的字符串表示形式。也许你想要

str

而不是

["{} ".format(i) for i in CMD]

甚至

str(CMD)

甚至更多

["{} ".format(i) for i in CMD if i != 0]