Reputation: 27
I'm new to Python and am having issues stripping excess material off a string that has been converted from a byte array. The code is reading data from a barcode reader and then comparing it to a lookup table read in from a file(the code stores lookup table as an array of lists). The barcode data is kept in a byte array read in from pyserial that I am trying to convert to a string so it can be compared.
File read:
with open('LUT.csv', 'rt') as csvfile:
lutFile = csv.reader(csvfile, delimiter=',')
for line in lutFile:
# the line is a list, append it to our lutTable list so that
# when we are done scanning the file we will have the
# complete list in memory.
self.lutTable.append(line)
Barcode conversion:
if gotBarcode:
# We have received a barcode, see if we recognize it.
#model = self.byteArray.decode("utf8") <<-- This does not work either...
modelString = str(self.byteArray, encoding='utf-8', errors='strict')
modelString.strip(' \t\n\r')
self.modelNumber.config(text="%s" % modelString)
self.serialCount = 0
searchIndex = 0
for i in self.lutTable:
if (modelString == self.lutTable[searchIndex][0]):
fileName = self.lutTable[i][0]
break;
else:
print("Lut check:%d: model:%s---, table:%s---" %(searchIndex, modelString, self.lutTable[searchIndex][0]))
fileName = 0;
searchIndex += 1
del self.byteArray[:]
print("%s" % searchIndex)
if fileName:
self.FileName.config(text="%s" % fileName)
Barcode as seen on the serial port:
Transfers data from a COM port to a client (COM30) - 13 bytes of 13
STATUS_SUCCESS
39 30 30 30 30 42 34 20 20 20 20 20 09 90000B4
Barcode data as seen from the print in the code which also shows the data from the lookup table: Read port...
Lut check:0: model:90000B4 ---, table:90000B1---
Lut check:1: model:90000B4 ---, table:90000B10---
Lut check:2: model:90000B4 ---, table:90000B2---
The barcode data coming in from the serial port is 13 bytes long and it appears that after the conversion from a byte array and attempted strip of spaces, the printed string is still 13 characters wide(it is hard to tell on this post but there are 6 extra characters after the "model:90000B4".) I can't really tell what the extra 6 characters are but they are causing the compare to fail.
Platform specifics: Windows 7, Python 3.5.4
So what is incorrect/missing about the modelString.strip(' \t\n\r')
line?
Upvotes: 0
Views: 1415
Reputation:
You need to assign the result of modelString.strip(' \t\n\r')
back to modelString
like so:
modelString = modelString.strip(' \t\n\r')
Upvotes: 3