字符串 strip() 不工作

String strip() not working

我是 Python 的新手,在从字节数组转换而来的字符串中剥离多余的 material 时遇到问题。该代码从条形码 reader 读取数据,然后将其与从文件读取的查找 table 进行比较(代码将查找 table 存储为列表数组)。条形码数据保存在从 pyserial 读取的字节数组中,我试图将其转换为字符串以便进行比较。

文件读取:

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)

条码转换:

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)

在串行端口上看到的条码:


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

从代码打印中看到的条形码数据也显示了查找中的数据 table: 读取端口...

Lut check:0: model:90000B4      ---, table:90000B1---
Lut check:1: model:90000B4      ---, table:90000B10---
Lut check:2: model:90000B4      ---, table:90000B2---

从串口传来的条码数据是13个字节长,看来是字节数组转换后尝试去掉空格,打印出来的字符串还是13个字符宽(网上看不太出来)这个 post 但在 "model:90000B4" 之后还有 6 个额外的字符。)我真的不知道额外的 6 个字符是什么,但它们导致比较失败。

平台细节:Windows7,Python3.5.4

那么 modelString.strip(' \t\n\r') 行的 incorrect/missing 是什么?

您需要像这样将 modelString.strip(' \t\n\r') 的结果分配回 modelString

modelString = modelString.strip(' \t\n\r')