'Python ValueError: 'dictionary update sequence element #0 has length 4; 2 is required'
I have created a method that fetches the data from the database, converts it into json format and returns the JSON response.
def getEchoResource(self):
try:
row = self.cursor.execute("SELECT * FROM echo_resource_log WHERE DATE(last_update) = CURDATE();")
if row:
response = app.response_class(response=json.dumps(dict(self.cursor.fetchall())), status=200, mimetype='application/json')
return response
except MySQLdb.Error as e:
logger.error("Error %d: %s" % (e.args[0],e.args[1]))
except Exception, e:
logger.error("Error : ", str(e))
The method throws this error message - ValueError: 'dictionary update sequence element #0 has length 4; 2 is required'
Trace -
>/app/worker/echo/apps/opsware_flask_rest_app/opsware_flask_rest_app/
updateEchoResource.py(123)getEchoResource()
-> row = self.cursor.execute("SELECT * FROM echo_resource_log WHERE
DATE(last_update) = CURDATE();")
(Pdb) n
>/app/worker/echo/apps/opsware_flask_rest_app/opsware_flask_rest_app/
updateEchoResource.py(124)getEchoResource()
-> if row:
(Pdb) n
>/app/worker/echo/apps/opsware_flask_rest_app/opsware_flask_rest_app/
updateEchoResource.py(125)getEchoResource()
-> response =
app.response_class(response=json.dumps(dict(self.cursor.fetchall())),
status=200, mimetype='application/json')
(Pdb) n
ValueError: 'dictionary update sequence element #0 has length 4; 2 is
required'
>/app/worker/echo/apps/opsware_flask_rest_app/opsware_flask_rest_app/
updateEchoResource.py(125)getEchoResource()
row returned -
('n3pvap168', 'X2Linux_NSS', 'Contact does not exist in Contacts table', datetime.datetime(2017, 7, 21, 4, 27, 37))
Solution 1:[1]
A Python dictionary as you probably know is an unordered set of key-value pairs. So each entry in a dictionary must have one key and one value. What is happening here is that you are trying to convert a sequence of four objects into a dictionary as shown by your row (hence the error expected length 2, got length 4).
To fix this, you will need to convert your 4-length data structure into a 2-length one. This can be done by creating an array of length 2 and storing the string you want to use as a key in the 0-index of the array and the rest of your row in the 1-index. You might be able to achieve this using slicing.
Solution 2:[2]
You likely have to zip() before you dict(), using:
dict(zip(...))
In your case, with many rows, try:
{dict(zip(row))
for row in cursor.fetchall()}
I could not test this! You might change the code, especially I am not sure whether the dict comprehension will be accepted.
With this guess, it would make:
def getEchoResource(self):
try:
row = self.cursor.execute("SELECT * FROM echo_resource_log WHERE DATE(last_update) = CURDATE();")
if row:
response = app.response_class(response=json.dumps({dict(zip(row))
for row in cursor.fetchall()}), status=200, mimetype='application/json')
return response
except MySQLdb.Error as e:
logger.error("Error %d: %s" % (e.args[0],e.args[1]))
except Exception, e:
logger.error("Error : ", str(e))
If that does not work, have a look at the following:
- Displaying database results in Python Flask: ValueError: dictionary update sequence element #0 has length 6; 2 is required to see an example of how to zip the dict if such an error pops up
- which was originally taken from Output pyodbc cursor results as python dictionary --> recommended!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ziyad Edher |
| Solution 2 |
