Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8484151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:18:02+00:00 2026-06-10T20:18:02+00:00

In order to improve readability in my code I want to use plain words

  • 0

In order to improve readability in my code I want to use plain words instead index numbers, when creating JSON object:

this is my database table school_subjects:

mysql> DESCRIBE school_subjects;
+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | int(11)      | NO   | PRI | NULL    | auto_increment |
| name             | varchar(500) | NO   |     | NULL    |                |
| user_id          | int(11)      | NO   | MUL | NULL    |                |
| created_by       | varchar(64)  | NO   |     | NULL    |                |
| created_time     | datetime     | NO   |     | NULL    |                |
| num_of_followers | int(11)      | NO   |     | NULL    |                |
+------------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> 

my python code:

serg@serg-PORTEGE-Z835:~$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import simplejson as json
>>> import MySQLdb
>>> import collections
>>> 
>>> mydb = MySQLdb.connect(host='localhost', user='root', passwd='', db='schooldb')
>>> cursor = mydb.cursor()
>>> cursor.execute("""
...                     SELECT id, name
...                     FROM school_subjects
...             """)
6L
>>> rows = cursor.fetchall()
>>> result = []
>>> for row in rows:
...     d = dict()
...     d['id'] = row.id        #I want something similar to this
...     d['name'] = row.name    #but it doesn't work
...     result.append(d)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'tuple' object has no attribute 'id'

as you can see I’m getting this error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'tuple' object has no attribute 'id'

however this code works fine:

>>> result = []
>>> for row in rows:
...     d = dict()
...     d['id'] = row[0]
...     d['name'] = row[1]
...     result.append(d)
... 
>>> subjects = json.dumps(result, indent=4)
>>> print subjects
[
    {
        "id": 1,
        "name": "Math 140"
    },
    {
        "id": 2,
        "name": "English 102"
    },
    {
        "id": 3,
        "name": "CS 240"
    },
    {
        "id": 4,
        "name": "CS 210"
    },
    {
        "id": 5,
        "name": "Math 140"
    },
    {
        "id": 6,
        "name": "English 102"
    }
]
>>> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:18:04+00:00Added an answer on June 10, 2026 at 8:18 pm

    The cursor object has a .description attribute that tells you the names of each column, use that to turn a row into a dict:

    cursor.execute("""
                        SELECT id, name
                        FROM school_subjects
                """)
    columns = [desc[0] for desc in cursor.description]
    result = []
    for row in rows:
        row = dict(zip(columns, row))
        result.append(row)
    

    For details on the .description attribute, see the Python DB API 2.0 specification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to improve code readability, sometimes I use alias methods. These are basically
We use MYSQL InnoDB engine for insert and update operations, in order to improve
in order to recover data from server I use XMLHttpRequest and my code is
I’m looking at ways to improve the consistency, brevity, and readability of some code
How can I use two devices in order to improve for example the performance
In order to improve user experience we want the ability to have an animated
In order to improve type safety in some C library code, I got the
In order to improve performance i am creating subviews for my scroll view in
In order to improve the maintainability of a website, is it a good idea
I'm going to enable hibernate.order_inserts in order to improve my batch insert operations. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.