Tag object reading
Hello all,
I have used the sample code within getting started to try and creating a program which can retrieve the unique id of a type 2 tag to be stored within a database along with a username. However, when I attempt to remove the the "Type 2 Tag=" part of the returned data from the tag function, I am unable to iterate over this as the object is not iterable. I can however force this data to be a string and then parse the result. My question is this: is there a way to retrieve only the id attribute itself, or is it a better idea to force the result to be a string and then use this within my solution?
Thanks,
Matt
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- nfcpy Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- Stephen Tiedemann
- Solved:
- 2015-09-19
- Last query:
- 2015-09-19
- Last reply:
- 2015-09-18
I'm not sure how you attempted to get the ID but here's a code snippet that print's the tag identifier, and then the tag object representation and the formatted string.
>>> import nfc
>>> clf = nfc.Contactless
>>> tag = clf.connect(
>>> tag.identifier
'\x04\x94\
>>> repr(tag)
'<nfc.tag.
>>> str(tag)
"Type2Tag 'NXP NTAG216' ID=0494FFC2D73880"
Matthew Crown (matthewcrown) said : | #2 |
Thanks for the reply. Still just need a little more clarification:
When I use
>>> print(repr(tag))
>>>print(str(tag))
this works fine but when I run
>>> print(tag.
this doesn't appear to give an output - it does give me some sort of issue with regards the character coding though which I'm unsure about. Can you tell me will tag.identifier work for all cards or only some?
The tag.identifier is a raw byte string. If you want to print use a hexadecimal encoding:
>>> print tag.identifier.
The tag.identifier works for all cards.
Matthew Crown (matthewcrown) said : | #5 |
Thanks Stephen Tiedemann, that solved my question.