我有一個查詢。我的查詢回傳我想要作為字典的結果。我使用 python 3.7 和 mysql。我在 ubuntu 18 上喜歡:
[{'community_string': 'public'}, {'community_string': 'private'}]
我只想使用 public 和 private 作為字串。為此,我撰寫了這樣的代碼:
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('[','')
但它給了我AttributeError: 'list' object has no attribute 'values'錯誤。我該如何解決這個問題?
uj5u.com熱心網友回復:
不清楚,你想做什么。你想得到“community_string”的值嗎?
for r in cursor.fetchall():
print(r['community_string'])
我不明白你對 json 和字串替換操作做了什么......
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415211.html
標籤:
