简体   繁体   English

输出是像 [{a:b, d:c,....},{a:f, b:q,...},{a:v, b:k,....}, 的元组。 ..]

[英]output is tuple like [{a:b, d:c,....},{a:f, b:q,...},{a:v, b:k,....},...]

Using mySQL I am running this: (Getting data and looping it through a table)使用 mySQL 我正在运行:(获取数据并在表中循环)

users is the table.用户是表。 The console is outputting a tuple.控制台正在输出一个元组。

db = MySQLdb.connect("username.mysql.pythonanywhere-services.com", "username", "password", "username$database")
cursor = db.cursor()    
cursor.execute('SELECT uid, firstname, lastname, username, city, state, streetaddress, email, special_needs, phonenumber, zipcode FROM users')
results = cursor.fetchall()
print(results)

I am getting this back:我要回来了:

[{'special_needs': 'N/A', 'phonenumber': '5555555555', 'city': 'Oakland', 'zipcode': '44444', 'streetaddress': '32323 Apple St.', 'state': 'CA', 'email': 'bernie@taylor.com', 'uid': 1, 'firstname': 'Bernie', 'lastname': 'Sanders', 'username': 'bernieswift'}, {'special_needs': 'N/A', 'phonenumber': '6666666666', 'city': 'Greenappple', 'zipcode': '77777', 'streetaddress': '24936 Calaroga Ave.', 'state': 'CA', 'email': 'green@grass.com', 'uid': 2, 'firstname': 'john', 'lastname': 'lost', 'username': 'tony'}]

What I was doing before: ( this works locally )我之前在做什么:(这在本地有效)

print_str = "<table>"
for result in results:
print_str += "<tr><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><td> %s <br></td><tr>" % (result['uid'], result['firstname'], result['lastname'], result['username'], result['city'], result['state'], result['streetaddress'], result['email'], result['special_needs'], result['phonenumber'], result['zipcode'])
    print_str += "</table>"

How do I get specific values for uid, firstname, etc?如何获取 uid、firstname 等的特定值? (assuming I want to iterate through the whole database) How do I get the individual values from the database? (假设我想遍历整个数据库)如何从数据库中获取单个值? I'm really confused by tuple.我真的对元组感到困惑。

I think you can do something like this.我认为你可以做这样的事情。 for example, you want firstname where uid is equal to 1例如,您想要 uid 等于 1 的名字

uname = [counter['firsname'] for counter in result if counter['uid'] == 1] which returns ['Bernie'] uname = [counter['firsname'] for counter in result if counter['uid'] == 1]返回['Bernie']

This works with more complicated statements for example:这适用于更复杂的语句,例如:

data = [(counter['firstname'],counter['lastname']) for counter in result if counter['city'] in ['Oakland', 'Greenappple']]

returns [('Bernie', 'Sanders'), ('john', 'lost')]返回[('Bernie', 'Sanders'), ('john', 'lost')]

Although this is not the perfect solution it gets the job done.虽然这不是完美的解决方案,但它可以完成工作。

You need to initialise your database removing DictCursor.您需要初始化数据库以删除 DictCursor。

You will have something like the following:您将获得如下内容:

`MySQLdb.connect(user='joe', passwd='password', db='dbname',
                      cursorclass=MySQLdb.cursors.DictCursor)`

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

相关问题 如何打印 \0 \a \b \v \f 等字符 - How to print characters like \0 \a \b \v \f 正则表达式a:bcda:b - Regex a: b c d a:b 优雅的解析呢? “ a,b,c”,d,“ e,f” - Elegant parsing of this? “a,b,c”,d,“e,f” 为什么元组(set([1,“a”,“b”,“c”,“z”,“f”]))==元组(set([“a”,“b”,“c”, “z”,“f”,1]))85%的时间启用了哈希随机化? - Why does tuple(set([1,“a”,“b”,“c”,“z”,“f”])) == tuple(set([“a”,“b”,“c”,“z”,“f”,1])) 85% of the time with hash randomization enabled? 如何在 Python 中将 abcd 转换为类似 {&#39;a&#39;: {&#39;b&#39;: {&#39;c&#39;: {&#39;d&#39;:}}}} 的内容? - How do I convert a.b.c.d into something like this {'a': {'b': {'c': {'d':}}}} in Python? 如何表示约束(A &amp;&amp; B)|| (C&amp;&amp;D) || (E &amp;&amp; F) || - How to represent the constraint (A && B) || (C && D) || (E && F) || 多个变量的赋值(“a,b = c,d”与“a = c, b = d”) - Assignment of Multiple Variables ("a,b = c,d" vs "a = c , b = d") 这个东西[(a, ),(b, ),(c, ),(d, )]怎么转换成[a,b,c,d]呢? - How to convert this thing [(a, ),(b, ),(c, ),(d, )] into [a,b,c,d]? 在 python 中使用 b'' like f 字符串 - Using b'' like f string in python 将 list 的元素从 [['a', 'b'], ['c', 'd']] 转换为 ['a b', 'c d'] - Transform the elements of list from [['a', 'b'], ['c', 'd']] to ['a b', 'c d']
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM