python-oracle将结果放入字典并匹配给定的lis

2024-04-29 16:14:43 发布

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

SQL Results

如何将sql结果中的给定列表匹配到字典中? 这是我的密码:

import cx_Oracle

con = cx_Oracle.connect('fmstech','fmstech','fmsdev')
cur = con.cursor()
cur.execute("select * from num_prefix where network = 'GLOBE'")

globe = ['639988800000', '639066256904', '0150422 153023']
results = {}
for lines in cur:
    results[lines[0]]=lines[1]
    globe1 = globe[1][2:5]
    if globe1 in results:
        print '906 is exist'
    else:
        print '906 is not exist'

cur.close()
con.close()

但是我得到了结果:906 is not exist


Tags: incloseisnotconresultsexistoracle
1条回答
网友
1楼 · 发布于 2024-04-29 16:14:43

解决办法就是这样。使用int()

globe1 = int(globe[1][2:5])

示例1:不带out int()

results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = globe[1][2:5]
if globe1 in results:
        print '906 is exist'
else:
        print '906 is not exist'

o/p公司

906 is not exist

示例2:with int()

results = {817: 'globe', 906: 'globe'}
globe = ['639988800000', '639066256904', '0150422 153023']
globe1 = int(globe[1][2:5])
if globe1 in results:
        print '906 is exist'
else:
        print '906 is not exist'

o/p公司

906 is exist

原因:

在dict中,这里的键是数字,而在代码中,as globel是字符串。所以把它转换成数字然后比较

相关问题 更多 >