Python Forum
working with TLV module Object
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
working with TLV module Object
#1
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
Reply
#2
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 end
Make it to .csv file and import to Excel,i would have used Pandas then can drop Excel or if need to use to_excel.
Jennifer_Jone likes this post
Reply
#3
The entries in final_output are of type tlv8.Entry. There is dataattribute 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)
Output:
['Arnon Plastic Industries Co. Ltd.', '300498609900003', '2023-02-14 12:48:13', '4600.00', '600.00'] ['Arnon Plastic Industries Co. Ltd.', '300498609900003', datetime.datetime(2023, 2, 14, 12, 48, 13), 4600.0, 600.0]
Always start by reading the docs and if needed - also the source code.
Jennifer_Jone and snippsat like this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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. Heart

Regards,
Jenni
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unsure why module is not working garynewport 0 771 Feb-15-2023, 03:21 PM
Last Post: garynewport
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,595 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  Pandas module not working Hass 2 1,726 Apr-14-2022, 03:13 PM
Last Post: snippsat
  TypeError: 'module' object is not callable SyamPothan 4 4,682 Mar-04-2022, 04:44 PM
Last Post: SyamPothan
  'module' object is not callable Racer_x 5 2,744 Nov-04-2021, 03:20 PM
Last Post: Racer_x
  (TypeError: 'module' object is not callable) for getopt Module nnevarez 2 2,890 Jul-03-2020, 01:45 AM
Last Post: nnevarez
  importing module - not working jdhamblett 3 3,026 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  setup() from turtle module not working bobfat 7 6,144 Oct-28-2019, 11:05 AM
Last Post: newbieAuggie2019
  pyinstaller not working with acoustics module pynz 2 2,767 Oct-08-2019, 07:56 AM
Last Post: pynz
  'module' object is not callable psosmol 5 5,168 Apr-15-2019, 07:16 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020