Python解码十六进制是错误TypeError:奇数长度字符串

时间:2015-05-28 08:49:12

标签: python hex base64 decode typeerror

我的源代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64

PACKETS_FILE = './data/packets.txt'

def a112(fp):
    hline, prev = '', None
    with open(fp, 'r') as f:
        for line in f:
            if not 'Name:' in line: continue
            hx = line.strip().split(' ')[1].split('.')[0]
            if hx == prev: continue
            prev = hx
            hline += hx
    b = hline.decode('hex')
    b = base64.b64decode(b)
    print(b)

if __name__ == '__main__':
    a112(PACKETS_FILE)

错误代码

Traceback (most recent call last):
  File "a112.py", line 21, in <module>
    a112(PACKETS_FILE)
  File "a112.py", line 16, in ch112
    b = hline.decode('hex')
  File "C:\Python27\lib\encodings\hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Odd-length string

我不理解。

0 个答案:

没有答案