Home > Mobile >  AttributeError: 'list' object has no attribute 'values' python select query
AttributeError: 'list' object has no attribute 'values' python select query

Time:01-19

I have a query. My query returns the result that i want as a dictionary. I use python 3.7 and mysql. Im on ubuntu 18 Like:

[{'community_string': 'public'}, {'community_string': 'private'}]

I want to use only public and private as a string. For doing it, i wrote my code like that:

query= "SELECT community_string FROM nat_snmp_string WHERE status ='enable'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
com_str = cursor.fetchall()
com_str_string = com_str.values()
cmnyt_str = json.dumps(list(com_str))
print(cmnyt_str)  
cmd_line = cmnyt_str.replace(':','')
cmd_line = cmd_line.replace('"','')
cmd_line = cmd_line.replace(']','')
cmd_line = cmd_line.replace('[','')
   

but it gives me AttributeError: 'list' object has no attribute 'values' error. How can I solve this issue?

CodePudding user response:

It isn't clear, what do you want to do. Do you want to get the value of "community_string"?

for r in cursor.fetchall():
   print(r['community_string'])

I don't understand what do you do with json and string replace operations...

  •  Tags:  
  • Related