处理Python 3中的MySQL结果

huangapple go评论69阅读模式
英文:

Processing mysql result in python 3

问题

Sure, here's the translated content:

我是论坛的新手,如果问题格式不太好,请多包涵。

我试图从MySQL数据库表中提取行,并在处理列(其中一列包含需要展开的JSON)后打印出相同的内容。以下是源代码和预期输出。如果有人能够建议一种更容易管理这些数据的方法,那将是很好的。

注意:我已经通过大量的循环和解析实现了这一点,但存在以下挑战:
1) col_names和data之间没有连接,因此当我打印数据时,我不知道结果集中数据的顺序,因此我打印的列标题与数据不匹配,是否有办法保持这种同步?
2) 我希望能够在不进行太多重做的情况下灵活地更改列的顺序。

实现这一目标的最佳方法是什么?我还没有探索过pandas库,因为我不确定是否真的有必要。

使用Python 3.6

表中的示例数据
-------------------------
    id, student_name, personal_details, university
    1, Sam, {"age":"25","DOL":"2015","Address":{"country":"Poland","city":"Warsaw"},"DegreeStatus":"Granted"},UAW
    2, Michael, {"age":"24","DOL":"2016","Address":{"country":"Poland","city":"Toruń"},"DegreeStatus":"Granted"},NCU

我正在使用MySQLdb.connect对象查询数据库,步骤如下:

    query = "select * from student_details"
    cur.execute(query)
    res = cur.fetchall()  # 获取一个元组集合
    db_fields = [z[0] for z in cur.description]  # 生成列名列表

变量中的数据:

    >>> db_fields
    ['id', 'student_name', 'personal_details', 'university']
    >>> res
    ((1, 'Sam', '{"age":"25","DOL":"2015","Address":{"country":"Poland","city":"Warsaw"},"DegreeStatus":"Granted"}','UAW'),
     (2, 'Michael', '{"age":"24","DOL":"2016","Address":{"country":"Poland","city":"Toruń"},"DegreeStatus":"Granted"}','NCU'))

期望的输出:
----------------
 id, student_name, age, DOL, country, city, DegreeStatus, University
 1, 'Sam', 25, 2015, 'Poland', 'Warsaw', 'Granted', 'UAW'
 2, 'Michael', 24, 2016, 'Poland', 'Toruń', 'Granted', 'NCU'
英文:

I'm new to this forum, kindly excuse if the question format is not very good.

I'm trying to fetch rows from database table in mysql and print the same after processing the cols (one of the cols contains json which needs to be expanded). Below is the source and expected output. Would be great if someone can suggest an easier way to manage this data.

Note: I have achieved this with lots of looping and parsing but the challenges are.<br>

  1. There is no connection between col_names and data and hence when I am printing the data I don't know the order of the data in the resultset so there is a mismatch in the col title that I print and the data, any means to keep this in sync ?<br>
  2. I would like to have the flexibility of changing the order of the columns without much rework.

What is best possible way to achieve this. Have not explored the pandas library as I was not sure if it is really necessary.

Using python 3.6

Sample Data in the table

id, student_name, personal_details, university
1, Sam, {&quot;age&quot;:&quot;25&quot;,&quot;DOL&quot;:&quot;2015&quot;,&quot;Address&quot;:{&quot;country&quot;:&quot;Poland&quot;,&quot;city&quot;:&quot;Warsaw&quot;},&quot;DegreeStatus&quot;:&quot;Granted&quot;},UAW
2, Michael, {&quot;age&quot;:&quot;24&quot;,&quot;DOL&quot;:&quot;2016&quot;,&quot;Address&quot;:{&quot;country&quot;:&quot;Poland&quot;,&quot;city&quot;:&quot;Toruń&quot;},&quot;DegreeStatus&quot;:&quot;Granted&quot;},NCU

I'm querying the database using MySQLdb.connect object, steps below

query = &quot;select * from student_details&quot;
cur.execute(query)
res = cur.fetchall()  # get a collection of tuples 
db_fields = [z[0] for z in cur.description]  # generate list of col_names

Data in variables:

&gt;&gt;&gt;db_fields
[&#39;id&#39;, &#39;student_name&#39;, &#39;personal_details&#39;, &#39;university&#39;]
&gt;&gt;&gt;res
((1, &#39;Sam&#39;, &#39;{&quot;age&quot;:&quot;25&quot;,&quot;DOL&quot;:&quot;2015&quot;,&quot;Address&quot;:{&quot;country&quot;:&quot;Poland&quot;,&quot;city&quot;:&quot;Warsaw&quot;},&quot;DegreeStatus&quot;:&quot;Granted&quot;}&#39;,&#39;UAW&#39;),
 (2, &#39;Michael&#39;, &#39;{&quot;age&quot;:&quot;24&quot;,&quot;DOL&quot;:&quot;2016&quot;,&quot;Address&quot;:{&quot;country&quot;:&quot;Poland&quot;,&quot;city&quot;:&quot;Toruń&quot;},&quot;DegreeStatus&quot;:&quot;Granted&quot;}&#39;,&#39;NCU&#39;))

Desired Output:

 id, student_name, age, DOL, country, city, DegreeStatus, University
 1, &#39;Sam&#39;, 25, 2015, &#39;Poland&#39;, &#39;Warsaw&#39;, &#39;Granted&#39;, &#39;UAW&#39;
 2, &#39;Michael&#39;, 24, 2016, &#39;Poland&#39;, &#39;Toruń&#39;, &#39;Granted&#39;, &#39;NCU&#39;

答案1

得分: 1

Here's the translated code portion:

# 一个不太Pythonic但容易理解的方法(也许你可以写一个更Pythonic的解决方案)可能是:

def unwrap_dict(_input):
    res = dict()
    for k, v in _input.items():
        # 假设你知道只有一个嵌套级别
        if isinstance(v, dict):
            for _k, _v in v.items():
                res[_k] = _v
            continue
        res[k] = v
    return res

all_data = list()
for row in result:
    res = dict()
    for field, data in zip(db_fields, row):
        # 假设你知道personal_details是唯一的JSON列
        if field == 'personal_details':        
            data = json.loads(data)
        if isinstance(data, dict):
            extra = unwrap_dict(data)
            res.update(extra)
            continue
        res[field] = data

    all_data.append(res)

Please note that I've retained the variable and function names in English as they are common in programming and not typically translated.

英文:

A not-too-pythonic way but easy to understand (and maybe you can write a more pythonic soltion) might be:

def unwrap_dict(_input):
    res = dict()
    for k, v in _input.items():
        # Assuming you know there&#39;s only one nested level
        if isinstance(v, dict):
            for _k, _v in v.items():
                res[_k] = _v
            continue
        res[k] = v
    return res


all_data = list()
for row in result:
    res = dict()
    for field, data in zip(db_fields, row):
        # Assuming you know personal_details is the only JSON column
        if field == &#39;personal_details&#39;:        
            data = json.loads(data)
        if isinstance(data, dict):
            extra = unwrap_dict(data)
            res.update(extra)
            continue
        res[field] = data

    all_data.append(res)

huangapple
  • 本文由 发表于 2020年1月6日 23:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615111.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定