【问题标题】:Python List not recognising values from a Database TablePython List 无法识别数据库表中的值
【发布时间】:2016-03-13 19:16:19
【问题描述】:

我想知道为什么代码没有识别出10 / 11的Field值在列表['10', '11']之内,以及为什么它绕过了最初的if语句。

任何可能的解决方案将不胜感激,在代码下方还有代码运行后产生的打印语句。

    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

不在列表中

【问题讨论】:

    标签: python mysql list pyodbc


    【解决方案1】:

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

    【讨论】:

    • 有什么办法可以修复列表,使其仅显示字符串 10 和 11?对于Field 部分(尽管为了方便我将其从提供的代码中删除),最初生成的值的格式如下:('10 ', )。我用Field.replace(" x ", "") 来编辑这个。这是否可以将 EList 值放入相同的格式,如果它们的格式相同(没有尾随空格、语音标记等),它会起作用吗?
    【解决方案2】:
    for Field in TableRow:
        Field = Field.strip()
        print(Field, "This is the currently looped ID")
        print(EList, "This is the EList")
    

    添加条以从字段中去除开始和结束空格

    【讨论】:

      猜你喜欢
      • 2020-12-17
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多