Remove Space from string data

Hello Everyone,
I am scanning a barcode from scanner and the data I receive is a string type, Now I see the data being populated but It has a space in front of data. So first character is space and then actual characters. “1Z096V4Y0657697557” Is there a way I can get rid of those extra characters? Interestingly I see boxes here but in actual tag it’s a space.

Thank you in advance.

You can use Python’s strip function, which will remove any spaces in the string.

I tried it but I think I might be doing it in a wrong manner as I don’t know python much. Let’s say I have
" Hello" in tag 9. What do I write? strip(tag9) or tag9.strip() ? and the result after strip will be stored in same tag9 ?

I’m assuming tag9 is a variable name. If that’s the case, then you can do this:

bar_str = tag9.strip()

Otherwise, you’ll first need to read in the tag value. One way you can do this is by using system.tag.readBlocking() or system.tag.readAsync().

I tried the same way. I think I know why it isn’t working. It shows me space in data value but when I copy that tag in notepad or here it looks like - “1Z096V4Y0657697557”.even this website is removing those characters. I am not sure if I can upload a picture here. As you see it’s not a space, It’s a character which is not readable by tag and it’s showing space.

Is there a command to delete first and last character from received string?

test = 'First Character'
print test[1:]

test = 'Last Character'
print test[:-1]

output

>>> 
irst Character
Last Characte
>>> 
2 Likes

Wow. This was big help, I tried and it works.

tag1 = system.tag.read("[default]Barcode Scanner/Barcode Scanner Data")

tag2 = tag1.value
print tag2

tag3 = tag2[1:-1]

print tag3

Somewhat safer would be to print repr(tag1) (to see the explicit character code(s) used as the prefix and suffix) and then pass them to strip, as in tag2.strip("\x01")

1 Like

@urvishmehta92 you can upload by clicking the upload button…

1

For code and formatted text attach by using preformatted text…

2

OMG! I don’t know how I missed it. :man_facepalming:

Thank you for pointing that out.

1 Like