Python列表无法识别数据库表中的值

时间:2016-03-13 19:16:19

标签: python mysql list pyodbc

我想知道为什么代码无法识别10/11的字段值是否在列表中[' 10',' 11'],以及为什么它会绕过最初的if语句。

我们非常感谢任何可能的解决方案,代码下面还有代码运行后产生的print语句。

    TableRow = DBSelect.fetchall()
    for Field in TableRow:
        print(Field, "This is the currently looped ID")
        print(EList, "This is the EList")
        if Field in EList : # compares each  ID against the EList
            print(EList, "This is pre- modified EList")
            EList.pop([Field]) # removes the ID from EList
            print(EList, "This is post- modified EList")
            UnavailableE = UnavailableE + 1 #Add to the number of unavailable E
            print(UnavailableE, "This is the Unavailable e in the loop")
        elif UnavailableE == ECount: # if the UnavailableE is Equal to  ECount, then no ID available - runs this output
            print("No e available")
        else:  
            print("Not in list")

为了进一步参考,以下是由此产生的印刷语句:

10这是当前的循环ID

[' 10',' 11']这是EList

不在列表中

11这是当前的循环ID

[' 10',' 11']这是EList

不在列表中

2 个答案:

答案 0 :(得分:1)

如果Field不是字符串(数字?)或字符串"10",则列表中的不是,因为列表包含字符串"10 ""11 "(请注意尾随空格)。

答案 1 :(得分:0)

for Field in TableRow:
    Field = Field.strip()
    print(Field, "This is the currently looped ID")
    print(EList, "This is the EList")

添加条带以开始从字段开始和结束空白