TypeError:字符串创建中一元+:“str”的操作数类型错误

2024-10-01 07:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在运行时不断遇到上述错误:

def Decode(iList):
    IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
                  ,"Master Card","Union Pay","Petroleum"]
    TypeList = ["Debit account", "Credit account"]
    for istr in iList:
        ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
        + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
        + ". The card is linked to a " + TypeList[int(istr[8])]
        + " with the account number: " + istr[8:]
        WriteFile(ostr)



File "", line 24, in Decode
    + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
TypeError: bad operand type for unary +: 'str'

我尝试了str()的错误线路,但没有成功


Tags: theinforon错误accountcardint
3条回答

您有一个有效、完整的Python行

    ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"

然后,另一行以一元+开头,这不是应用于字符串的有效语句

    + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]

有关在多行上继续表达式的选项,请参见How can I do a line continuation in Python

style guide中建议的做法是在运算符之前使用括号和break

def Decode(iList):
    IssuerList = ["Dummy","enRoute","JCB","Diner's Club","Visa"
                  ,"Master Card","Union Pay","Petroleum"]
    TypeList = ["Debit account", "Credit account"]
    for istr in iList:
        ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
            + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
            + ". The card is linked to a " + TypeList[int(istr[8])]
            + " with the account number: " + istr[8:])
        WriteFile(ostr)

假设python知道要将表达式带到下一行。在本例中,它不知道这一点,除非在行尾使用\告诉它,或者将表达式括在()中。基本上以某种方式表示看似完成的表达式不完整,并继续下一行

这:

        ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20"
        + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8]
        + ". The card is linked to a " + TypeList[int(istr[8])]
        + " with the account number: " + istr[8:]

应该是:

        ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
               + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
               + ". The card is linked to a " + TypeList[int(istr[8])] \
               + " with the account number: " + istr[8:]

或:

        ostr = (istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" 
                + istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] 
                + ". The card is linked to a " + TypeList[int(istr[8])] 
                + " with the account number: " + istr[8:])

现在让我解释一下您得到的实际错误“TypeError:一元+的错误操作数类型:”“str'in string creation”是因为它已经完成了对看似完整的第一行或上面删掉的部分的解析,现在又开始解析第二行。它看到新行以+开头,因此它必须是一元数+,因为+的左边没有操作数。它不适用于下一个字符串操作数,因此会出现问题

在多行上创建字符串时添加反斜杠可以解决以下问题:

ostr = istr + ": Was issued by " + IssuerList[int(istr[1])] + " in 20" \
+ istr[2:4] + ". The card expires on " + istr[4:6] + "/" + istr[6:8] \
+ ". The card is linked to a " + TypeList[int(istr[8])] \
+ " with the account number: " + istr[8:]

相关问题 更多 >