So, I have a pandas dataframe which is classified as 'object', but is actually base64 encoded payload, which I want to convert into hex.
raw
AIgIm/H/SfwAR2IBAgMgAgIAAQMCAFoAcQAAAAAAAAFxAAAAAAAAA4gAAABiAF8AABI=
AIgIm/v/SfsAUNwBAgMgAgIAEgMCAEIAcQAAAAAAAAFxAAAAAAAAA4gAAABkAF8AAAw=
AIgIm/z/Sg4AVroBAgMgAgIA6wMCAFgAcQAAAAAAAAFxAAAAAAAAA4geAAFEADoAGQs=
Using https://cryptii.com/base64-to-hex的属性值插入我得到了这些值(这是我期望的值):
new_raw
00 88 08 9b f1 ff 49 fc 00 47 62 01 02 03 20 02 02 00 01 03 02 00 5a 00 71 00 00 00 00 00 00 01 71 00 00 00 00 00 00 03 88 00 00 00 62 00 5f 00 00 12
00 88 08 9b fb ff 49 fb 00 50 dc 01 02 03 20 02 02 00 12 03 02 00 42 00 71 00 00 00 00 00 00 01 71 00 00 00 00 00 00 03 88 00 00 00 64 00 5f 00 00 0c
00 88 08 9b fc ff 4a 0e 00 56 ba 01 02 03 20 02 02 00 eb 03 02 00 58 00 71 00 00 00 00 00 00 01 71 00 00 00 00 00 00 03 88 1e 00 01 44 00 3a 00 19 0b
基于先前提出的类似问题,我尝试过:
df['new_raw'] = df['raw'].apply(lambda x: x.decode("base64").encode("hex"))
但这给了我
AttributeError: 'str' object has no attribute 'decode'` error.
答案 0 :(得分:1)
您具有Python 3字符串对象,该对象没有.hex()
方法。解码是对字节值进行操作,以获取字符串,这些字符串将编码。看来您发现一些Python 2特定的代码执行了不兼容的转换。
要将Base64数据转换为二进制,然后转换为十六进制,请使用base64
module,然后调用import base64
df['raw'].apply(lambda b: base64.b64decode(b).hex())
方法:
>>> import pandas as pd
>>> import base64
>>> df = pd.DataFrame({'raw': '''\
... AIgIm/H/SfwAR2IBAgMgAgIAAQMCAFoAcQAAAAAAAAFxAAAAAAAAA4gAAABiAF8AABI=
... AIgIm/v/SfsAUNwBAgMgAgIAEgMCAEIAcQAAAAAAAAFxAAAAAAAAA4gAAABkAF8AAAw=
... AIgIm/z/Sg4AVroBAgMgAgIA6wMCAFgAcQAAAAAAAAFxAAAAAAAAA4geAAFEADoAGQs=
... '''}, index=[0, 1, 2])
>>> df['raw'].apply(lambda b: base64.b64decode(b).hex())
0 0088089bf1ff49fc00476201020320020200010302005a...
1 0088089bf1ff49fc00476201020320020200010302005a...
2 0088089bf1ff49fc00476201020320020200010302005a...
Name: raw, dtype: object
演示:
{{1}}