Python中文
首页
教程
问答
标签
搜索
登录
注册
在Python中有一个迭代词典
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个数据库,我在其中注册了一些数据。我试图选择所有的行,并试图把我的表中的每一行放入字典,但我似乎不能做到这一点。你知道吗</p> <p>代码如下:</p> <pre><code>db = MySQLdb.connect("localhost","root","aqw","PFE_Project" ) cursor = db.cursor() sql = "SELECT * FROM ServerComponents" try: cursor.execute(sql) results = cursor.fetchall() nbre_row = cursor.rowcount server_name = [] server_price = [] for row in results: server_id = row[0] server_name.append(row[1]) core_number = int(row[2]) clock_speed = int(row[3]) ram = int(row[4]) rom = row[5] hdd = int(row[6]) video_card = row[7] cache_memory = row[8] # calculation metric is a function that i used to calculate the server prize p = calculations_metric (core_number, clock_speed, ram, hdd, video_card) server_price.append(p) try : # i wanna the attribute "response" be iterative response = {"Server Name" : server_name , "Server Price" : server_price } except : print "error in response" except: print "Error: unable to fetch data" print(response) </code></pre> <p>这是我得到的结果:</p> <pre><code>{"Server Name": ["Dell1", "Dell2"], "Server Price": [149, 151]} </code></pre> <p>但我想看到的结果是:</p> <pre><code>{"Server Name" : Dell1, "Server Price": 149} {"Server Name" : Dell2, "Server Price": 151} </code></pre> <p>有人能帮我吗?你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<pre><code>servers = [] for row in results: result = {} result['server_name'] = row[1]) p = calculations_metric (core_number, clock_speed, ram, hdd, video_card) result['server_price'] = p servers.append(result) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
jupyter运行一个旧的pytorch版本
4 回答
Jupyter运行不同版本的卸载库?
7 回答
Jupyter运行指定的键盘快捷键
9 回答
Jupyter通过.local文件“逃逸”virtualenv。我该如何缓解这种情况?
1 回答
Jupyter重新加载自定义样式
5 回答
Jupyter错误:“没有名为Jupyter_core.paths的模块”
3 回答
jupyter错误:无法在随机林中将决策树视为png
10 回答
Jupyter错误'内核似乎已经死亡,它将自动重新启动'为一个给定的代码块
3 回答
Jupyter错误地用阿拉伯语和字母数字元素显示Python列表
4 回答
Jupyter隐藏数据帧索引,但保留原始样式
2 回答
Jupyter集线器:启动器中出现致命错误。。。系统找不到指定的文件
1 回答
Jupyther中相同值的相同哈希,但导出到Bigquery时不相同
10 回答
Jupy上Python的读/写访问问题
9 回答
jupy上没有模块cv
2 回答
Jupy上的排序错误
10 回答
Jupy中bqplot图形的紧凑布局
6 回答
Jupy中matplotlib plot的连续更新
4 回答
Jupy中Numpy函数的文档
9 回答
Jupy中Pandas的自动完成问题
8 回答
jupy中Qt后端的Matplotlib动画
10 回答