简体   繁体   English

解码Base64字符串

[英]Decoding Base64 string

I'm working with Python to do some string decoding and I am trying to understand what does this line of code... 我正在使用Python进行一些字符串解码,并且试图了解这行代码的含义...

for irradiance_data in struct.iter_unpack("qHHHHfff", irradiance_list_bytes):
                print(irradiance_data)

In my case irradiance_list_bytes is something like this 在我的情况下,irradiance_list_bytes就是这样

"\xf5R\x960\x00\x00\x00\x009\x0f\xb4\x03\x01\x00d\x00\xa7D\xd1BC\x8c\x9d\xc2\xb3\xa5\xf0\xc0\xaer\x990\x00\x00\x00\x000\x0f\xb2\x03\x01\x00d\x00\x8f+\xd1B\x81\x9c\x9d\xc2\xf7\xfb\xe6\xc0u\x96\x9c0\x00\x00\x00\x00.\x0f\xb1\x03\x01\x00d\x00\xfe\x81\xd3B\x8a\r\x9e\xc2\xb4\xe7\x01\xc1\x1a\x7f\x9f0\x00\x00\x00\x00*\x0f\xb0\x03\x01\x00d\x00Z\xf5\xd3B\xedq\x9e\xc2&\xa1\x03\xc1\x94\x82\xa20\x00\x00\x00\x00-\x0f\xb1\x03\x01\x00d\x00\xb6\x8f\xd3Bg\xdf\x9d\xc2\x00\xad\xfd\xc0#\x93\xa50\x00\x00\x00\x000\x0f\xb2\x03\x01\x00d\x00\x95n\xd4B\x1d'\x9e\xc2\x1dW\x01\xc1\xd3\xa1\xa80\x00\x00\x00\x001\x0f\xb2\x03\x01\x00d\x00\x1d\xbc\xd3B\xeb\xca\x9d\xc2s\xbf\xf2\xc0.\xaf\xab0\x00\x00\x00\x001\x0f\xb2\x03\x01\x00d\x00\x13\xad\xd4BJx\x9d\xc2G(\xfb\xc0.\xc2\xae0\x00\x00\x00\x007\x0f\xb4\x03\x01\x00d\x00\xd1\xc9\xd4BS\xb8\x9d\xc2\xf0\xd9\xf8\xc0"

And the message error is 消息错误是

AttributeError: 'module' object has no attribute 'iter_unpack'

I beleive that, I have to change "qHHHHfff" to another string format, but I don't understand ? 我相信,我必须将“ qHHHHfff”更改为另一种字符串格式,但是我不明白吗?

The complete code is here... 完整的代码在这里...

import os
import glob
import exiftool
import base64
import struct 

irradiance_list_tag = 'XMP:IrradianceList'
irradiance_calibration_measurement_golden_tag = 'XMP:IrradianceCalibrationMeasurementGolden'
irradiance_calibration_measurement_tag = 'XMP:IrradianceCalibrationMeasurement'

tags = [ irradiance_list_tag, irradiance_calibration_measurement_tag ]

directory = '/home/stagiaire/Bureau/AAAA/'

channels = [ 'RED' ]

index = 0

for channel in channels:
    files = glob.glob(os.path.join(directory, '*' + channel + '*'))
    with exiftool.ExifTool() as et:
        metadata = et.get_tags_batch(tags, files)
        for file_metadata in metadata:
            irradiance_list = file_metadata[irradiance_list_tag]
            irradiance_calibration_measurement = file_metadata[irradiance_calibration_measurement_tag]
            irradiance_list_bytes = base64.b64decode(irradiance_list)
            print(files[index])
            index += 1            

            for irradiance_data in struct.iter_unpack("qHHHHfff", irradiance_list_bytes):
                print(irradiance_data)

EDIT 编辑

So a stated by Strubbly, this is the solution for this question. 因此,斯特鲁布布利(Strubbly)表示,这是该问题的解决方案。

print struct.unpack("I",x[:4])

for i in range(8): start = 4 + i*28 print struct.unpack("qHHHHfff",x[start:start+28]) 对于范围(8)中的i:start = 4 + i * 28 print struct.unpack(“ qHHHHfff”,x [start:start + 28])

struct.iter_unpack is only available in Python 3 and you are using Python 2. There is no direct equivalent. struct.iter_unpack仅在Python 3中可用,而您正在使用Python2。没有直接等效项。 struct.unpack will unpack one lump of 28 bytes (with that format string). struct.unpack将解压缩一个28字节的块(带有该格式字符串)。 struct.iter_unpack will unpack multiples of 28 bytes in Python 3. struct.iter_unpack将在Python 3中解压缩28字节的倍数。

If your data was suitable for struct.iter_unpack with that format code then you could do something like this: 如果您的数据适合使用该格式代码的struct.iter_unpack ,则可以执行以下操作:

for i in range(0,len(x),28):
    print struct.unpack("qHHHHfff",x[i:i+28])

Unfortunately your sample data is not a multiple of 28 bytes long and so I would expect an error in Python 3 as well. 不幸的是,您的示例数据不是28个字节长的倍数,因此我预计Python 3也会出现错误。

Without knowing about your data it is hard to correct your code but, at a guess, you data might have 4 bytes of some other data at the front. 在不了解数据的情况下,很难纠正代码,但是,您可能会猜到数据的前面有4个字节的其他数据。 So that could be unpacked with something like this: 因此,可以使用以下内容解压缩:

print struct.unpack("I",x[:4])
for i in range(8):
    start = 4 + i*28
    print struct.unpack("qHHHHfff",x[start:start+28])

In this example I have guessed that the first four bytes are an unsigned int but I have no way of knowing if that is correct. 在此示例中,我猜测前四个字节是unsigned int但是我无法知道这是否正确。 More information is needed. 需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM