Python 使用findall时,TypeError:应为字符串或类似字节的对象

Python 使用findall时,TypeError:应为字符串或类似字节的对象,python,Python,我正在尝试实现这个类,但是getPostFix方法有问题。我不知道如何修复我遇到的错误 这是类中的str方法,仅供参考 # Returns the string representation of the Calc. def __str__(self): theCalc = "" theCalc += 'Input is: ' + self.getInput() + '\n' theCalc +

我正在尝试实现这个类,但是getPostFix方法有问题。我不知道如何修复我遇到的错误

这是类中的str方法,仅供参考

    # Returns the string representation of the Calc.
    def __str__(self):
        
        theCalc = ""
        theCalc += 'Input is: ' + self.getInput() + '\n'
        theCalc += 'Postfix input is: ' + self.getPostFix() + '\n'
        theCalc += 'Value is: ' + str(self.evaluate()) + '\n'
        
        return theCalc  
这就是我得到错误的地方

    def getPostFix(self):

        result = ""

        # stack used to create postfix string
        self.stack = MyStack()

        input_string = self.getInput()
        input_string_split = [x for x in re.split("(\d*\.?\d*)", input_string) if x != '']
        numbers = re.findall('([0-9.]+)', input_string_split, re.DOTALL)

        for i in input_string_split:

            if i in numbers:
                result += i


            elif isOperator(i):
                while True:
                    topItem = self.stack.getTop()
                    if self.stack.size == 0 or topItem == '(':
                        self.stack.push(i)
                        break
                    else:
                        precedence_i = getPrecedence(i)
                        precedence_topItem = getPrecedence(topItem)

                        if precedence_i > precedence_topItem:
                            self.stack.push(i)
                            break
                        else:
                            ipop = self.stack.pop()
                            result += ipop if ipop else ""
                            #result += self.stack.pop()

            elif i == '(':
                self.stack.push(i)

            elif i == ')':
                ipop = self.stack.pop()

                while ipop != '(':
                    result += ipop if ipop else ""
                    ipop = self.stack.pop()

            elif i == '=':
                ipop = self.stack.pop()
                result += ipop if ipop else ""
                #result += self.stack.pop()

        while not self.stack.size == 0:
            ipop = self.stack.pop()
            result += ipop if ipop else ""

        return result
这里是错误。它说它需要一个字符串,但input\u string\u不是拆分字符串列表吗?我不知道如何修理它

Traceback (most recent call last):
  File "xxx", line 9, in <module>
    print(calc)
  File "xxx", line 23, in __str__
    theCalc += 'Postfix input is: ' + self.getPostFix() + '\n'
  File "xxx", line 50, in getPostFix
    numbers = re.findall('([0-9.]+)', input_string_split, re.DOTALL)
  File "/usr/local/Cellar/python@3.9/3.9.2_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 241, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
现在for循环中的if语句是:

if isOperand(i):
  result += (i + ' ')
但是我得到下面的输出,它向我表明for循环的其余部分没有被执行。也就是说,它不是遍历列表并查找运算符或括号等

Input is: 3.2+.4*5.67/6.145=
Postfix input is: 3.2 .4 5.67 6.145 
Value is: None

Input is: 11.897/3.4+9.2-0.4*6.9/12.6-16.7=
Postfix input is: 11.897 3.4 9.2 0.4 6.9 12.6 16.7 
Value is: None

Input is: 234+34*65=
Postfix input is: 234 34 65 
Value is: None

下面的内容应该会有所帮助,我认为逻辑是错误的。您的
for
循环已经在整个“输入字符串分割”中进行了检查,因此在循环过程中会检查每一行。希望这有帮助,让我知道

之前:

input_string_split = [x for x in re.split("(\d*\.?\d*)", input_string) if x != '']
numbers = re.findall('([0-9.]+)', input_string_split, re.DOTALL)

for i in input_string_split:
  if i in numbers:
    result += i
之后:

input_string_split = [x for x in re.split("(\d*\.?\d*)", input_string) if x != '']
numbers = list()

for i in input_string_split:
  number = re.search('([0-9.]+)', i)
  if number != None: 
    numbers.append(i)
    result += i

正在将输入字符串作为列表传递给正则表达式,可以尝试:
numbers=re.findall(“([0-9.]+)”、“.join(Input_string_split)、re.DOTALL)
或将数字检查移动为循环中的单个变量并逐行检查?您已经知道根本原因了。现在,如何修复它取决于您想要的输出。一个简单的
“”。join(input\u string\u split)
将字符串列表转换为字符串。@xintral谢谢。你能告诉我怎么做第二部分吗?将检查移动到循环中?看起来唯一没有运行的部分是self.evaluate()方法。这是python的eval()方法的包装吗?@xintral实际上我知道该方法不起作用,因为我还没有编写它。当我说它不起作用时,我的意思是在getPostFix方法中,只有“if isOperand”方法似乎起作用,它不会再进一步。所以,它不是捕获操作符(“+-/*),括号等。我认为这与isOperand helper函数有关,但我不确定。我没有说明以下任何代码,因此可能需要调整pop/push或异常处理。我更新了我的问题,以说明我如何尝试实现您的方法。它正在执行第一个if语句,但不是ex因为某种我无法理解的原因而执行其余的任务!
input_string_split = [x for x in re.split("(\d*\.?\d*)", input_string) if x != '']
numbers = list()

for i in input_string_split:
  number = re.search('([0-9.]+)', i)
  if number != None: 
    numbers.append(i)
    result += i