Working with TLV module Object

Hello ,

I’m a python junior developer and I have a task to accomplish. I have a QR code which is made of TLV value encoded in base 64.

I have to parse this QR code to extract the values ( they are 5 ) and export the values into excel sheet , then do some math on some numbers ( the 4th and 5th value are floats ).

To achieve this task , I used 2 modules :
tlv8
base64

Here is the full code

import tlv8
import base64
 
base64QR= "ASFBcm5vbiBQbGFzdGljIEluZHVzdHJpZXMgQ28uIEx0ZC4CDzMwMDQ5ODYwOTkwMDAwMwMTMjAyMy0wMi0xNCAxMjo0ODoxMwQHNDYwMC4wMAUGNjAwLjAw"  
rawByte = base64.b64decode(base64QR)
 
structure1 = {
    1: tlv8.DataType.STRING,
    2: tlv8.DataType.STRING,
    3: tlv8.DataType.STRING,
    4: tlv8.DataType.STRING,
    5: tlv8.DataType.STRING
}
 
final_output = tlv8.decode(rawByte, structure1)
 
str_output= tlv8.format_string(final_output)
 
print(str_output)

This will result in the following output :
[
<1, Arnon Plastic Industries Co. Ltd.>,
<2, 300498609900003>,
<3, 2023-02-14 12:48:13>,
<4, 4600.00>,
<5, 600.00>,
]

I want to be able to extract the data from each field and convert them ( strings for 1st,2nd , date for 3rd , 4th,5th to be float ).

I don’t want to include space or > symbol which appear at the end of each field. I also don’t want the serial numbers at beginning.

The final job is to extract these values into excel sheet and calculate the values in forth columns at the end.

I would appreciate if anyone could help me.

Thanks and regards,
Jennifer

final_output is an instance of tlv8.EntryList and it’s list-like. The <...> that you see are the repr of each entry.

You can get all of the entries as strings with:

>>> [entry.data for entry in final_output]
['Arnon Plastic Industries Co. Ltd.', '300498609900003', '2023-02-14 12:48:13', '4600.00', '600.00']

The company name is final_output[0].data.

I don’t know what final_output[1].data is. It’s a numeric string, but is it a proper number (to be converted to an int for calculations) or an identifier (e.g. a phone number or a part number)?

final_output[2].data is a date/time in string form.

final_output[3].data and final_output[4].data are floats as strings.

1 Like

Hi Matthew , Thank you so much. Problem solved with your solution.

I feel sad though as I was not able to find this solution - I spend 2 days trying without luck.

Appreciate your prompt answer.

Regards,
Jenni