我尝试将ascii转换为十六进制值。但我的脚本有时工作,有时不起作用。我想知道为什么。代码如下:
ascii_ch = B13515068
for i in range(50): #In excel I have 50 row
ascii_ch = sheet['C%s'%(i+2)].value #after C2, convert hex
ascii_to_hex= "".join("{:02x}".format(ord(c)) for c in ascii_ch )
sheet['I%s'%(i+2)] = ascii_to_hex
wb.save('a.xlsx')
我想ascii_to_hex = 423133353135303638
答案 0 :(得分:1)
看起来并非所有单元格实际上都具有与之关联的值。当某个单元格没有值时,ascii_ch = sheet['C%s'%(i+2)].value
会将ascii_ch
设置为None
。在下一行中,您将迭代ascii_ch
。但迭代None
!
你可能想检查一下,如下:
for i in range(50): #In excel I have 50 row
ascii_ch = sheet['C%s'%(i+2)].value #after C2, convert hex
if ascii_ch is None:
# Maybe warn the user that a value is missing?
continue # go on to the next cell