python cx_Oracle疯狂元组

问题描述 投票:-3回答:1

结果: [('192.168.200.10',),('192.168.200.11',),('192.168.200.12',),('192.168.200.13',),('192.168.50.2',),('192.168。 50.70',),('192.168.50.9',)] 1

import cx_Oracle
con = cx_Oracle.connect('something')
cur = con.cursor()
cur.execute("something")
tab = []
for a in cur:
    tab.append(a)
print(tab)
print(len(tab[1]))
cur.close()
con.close()

我想把它变成干净ip的标签 [ “192.168.200.10”, “192.168.200.11”] 我在这里有一个问题,为什么tab [1]的长度为1?

python cx-oracle
1个回答
0
投票

你可以试试这个

result = [row[0] for row in tab]

产量

['192.168.200.10', '192.168.200.11', '192.168.200.12', '192.168.200.13', '192.168.50.2', '192.168.50.70', '192.168.50.9']

或者,您可以更改将结果附加到tab列表的方式。像这样

for a in cur:
    tab.append(a[0])

对于你的长度= 1个问题。这是因为元组中只有一个ip。因此你得到的长度为1

© www.soinside.com 2019 - 2024. All rights reserved.