working with TLV module Object - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: working with TLV module Object (/thread-39590.html) |
working with TLV module Object - Jennifer_Jone - Mar-13-2023 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 appear at the end of each field. I also don't want the serials 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 RE: working with TLV module Object - snippsat - Mar-13-2023 You should give a try to show some effort,here some hint. >>> import re >>> >>> first = '<1, Arnon Plastic Industries Co. Ltd.>' >>> re.search(r'<\d,\s(.*)>', first).group(1) 'Arnon Plastic Industries Co. Ltd.'This regex should work for all,then need to convert eg for date can use strptime Quote:The final job is to extract these values into excel sheet and calculate the values in forth columns at the endMake it to .csv file and import to Excel,i would have used Pandas then can drop Excel or if need to use to_excel. RE: working with TLV module Object - buran - Mar-14-2023 The entries in final_output are of type tlv8.Entry . There is data attribute to access the values you want. Now the conversion is bit cumbersome, it's interesting why e.g. last two entries are tlv8.DataType.STRING instead of tlv8.DataType.FLOAT import base64 from datetime import datetime import tlv8 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 } values = [item.data for item in tlv8.decode(rawByte, structure1)] print(values) values[2] = datetime.strptime(values[2], "%Y-%m-%d %H:%M:%S") values[-2:] = [float(value) for value in values[-2:]] print(values) Always start by reading the docs and if needed - also the source code.
RE: working with TLV module Object - Jennifer_Jone - Mar-14-2023 Hi Buran , Thanks for such perfect answer - I should have figured it out myself if I spend more time reading docs. My problem solved. Thank you so much. Regards, Jenni |