简体   繁体   English

列表内的单个逗号分隔的元组

[英]Single comma separated tuple inside of a list

Using pyodbc I'm working on dealing with executing stored procedures with outputs. 使用pyodbc,我正在处理执行带有输出的存储过程。

import pyodbc
conn = pyodbc.connect('DRIVER=' + driver + ';PORT=1433;SERVER=' + server +
                      ';PORT=1443;DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
cmd = conn.cursor()
sql = """\
Too long to post
"""
params = ('EOXH39020220', 'EOXH39020245', 'EOXH3E360011')
for item in params:
    cmd.execute(sql, item)
    rst = cmd.fetchall()
    print(rst)
conn.close

The output of a particular stored procedure I am testing with produces the following: 我正在测试的特定存储过程的输出将产生以下内容:

[(False, 1, 3, 2, 967, 6, 'ABC-DE-FGHI', 'LOREM', 'IPSUM', 'CLASS PRODUCT', 'F/P', 'LABELDESCRIPTION', 'N/A', 'DESCRIPTION', 'ASL66', 'ASL10', '3FE50712BA', 'ABC-DE-FGHI', 'PO', 'ITEM#', 'OEM#', 'F/P', '1')]
[(False, 1, 4, 1, 967, 7, 'ABC-DE-FGHI', 'LOREM', 'IPSUM', 'CLASS PRODUCT', 'F/P', 'LABELDESCRIPTION', 'N/A', 'DESCRIPTION', 'ASL66', 'ASL10', '3FE50712BA', 'ABC-DE-FGHI', 'PO', 'ITEM#', 'OEM#', 'F/P', '1')]
[(False, 1, 4, 2, 967, 8, 'ABC-DE-FGHI', 'LOREM', 'IPSUM', 'CLASS PRODUCT', 'F/P', 'LABELDESCRIPTION', 'N/A', 'DESCRIPTION', 'ASL66', 'ASL10', '3FE50712BA', 'ABC-DE-FGHI', 'PO', 'ITEM#', 'OEM#', 'F/P', '1')]

I can't use .split on it since it's not really a string. 我不能在它上使用.split ,因为它不是真正的字符串。 How can I split up the contents of a tuple inside of a list? 如何在列表中拆分元组的内容? What I am trying to do is break the results back out into variables to use. 我想做的是将结果分解成多个变量以供使用。 I'm not getting the results I expect to see. 我没有得到我期望看到的结果。 Ex: 例如:

testlist = [(False, 1, 3, 2, 967, 6, 'ABC-DE-FGHI', 'LOREM', 'IPSUM', 'CLASS 1 PRODUCT', 'F/P', 'DESCRIPTION', 'N/A', 'DESCRIPTION', 'ASL66', 'ASL10', '3FE50712BA', 'ABC-DE-FGHI', 'PO', 'ITEM#', 'OEM#', 'F/P', '1')]
for i in testlist[0]:
    print(testlist[0][i])

results: 结果:

False 1 2 3 错误1 2 3

As suggested by roganjosh, I completely missed what I needed in my loop. 正如roganjosh所建议的那样,我完全错过了循环中需要的东西。 first 5 items ex: 前5项,例如:

testlist = [(False, 1, 3, 2, 967, 6, 'ABC-DE-FGHI', 'LOREM', 'IPSUM', 'CLASS PRODUCT', 'F/P', 'LABELDESCRIPTION', 'N/A', 'DESCRIPTION', 'ASL66', 'ASL10', '3FE50712BA', 'ABC-DE-FGHI', 'PO', 'ITEM#', 'OEM#', 'F/P', '1')]

newlist = []

for x in testlist[0]:
    newlist.append(x)

print(newlist[:5])

[False, 1, 3, 2, 967] [False,1,3,2,967]

The question is a bit confused but we established the issue in comments. 这个问题有点混乱,但是我们在评论中确定了这个问题。 Your title is mostly accurate in what's being returned from fetchall() : a list containing a single tuple, not a dictionary as you go on to state. fetchall()返回的内容中,您的标题大体上是准确的:继续说明时,该列表包含一个元组, 而不是字典。

fetchall() is generally used when you expect multiple matches, where each row would be a tuple in the list (so you might consider using fetchone() as stated by @Barmar in the comments if you're always getting one result). 当您期望多个匹配项时,通常使用fetchall() ,其中每行将是列表中的一个元组(因此,如果您总是得到一个结果,则可以考虑使用@Barmar在注释中声明的fetchone fetchone() )。

In the comments, you state that you tried the following but it didn't work: 在评论中,您指出您尝试了以下操作,但没有成功:

for i in testlist[0]: 
    print(testlist[0][i])

It won't - testlist[0] gives you access to the tuple, but then you try to access that as though it were a dictionary with i as the key. 不会testlist[0]使您可以访问元组,但是随后您尝试访问它,就好像它是以i为键的字典一样。 Instead, i is the name that is being assigned to each item in that tuple. 相反, i是分配给该元组中每个项目的名称。

The solution is as simple as doing: 解决方案非常简单:

for i in testlist[0]: 
    print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM