I'm writing a script in Python and I'm having some problems, I need to run a command with parameters taken from the database, I do this (mysql.connector needs to be installed):
import mysql.connector import os mydb = mysql.connector.connect( host="localhost", user="name", password="pass", database="base" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM `employee` LIMIT 2") myresult = mycursor.fetchall() for row in myresult: os.system('command ' + row[1])
I have 3 questions:
import json # 一些JSON数据: x = '{ "name":"John", "age":30, "city":"New York"}' # 解析x: y = json.loads(x) # 结果是一个Python字典: print(y["age"])
If you want to know why, you really should use
subprocess
, please readthis
.In Python, the usage of
import <module>
is the same as the#include <module>
preprocessing ofC
/C
The instructions are very similar, although there are some differences. So you don't need to include import json, but instead import json.Anyway,
json
is a Python built-in module for parsing, encoding, indenting and writing.json
files, if you get aJSON
formatted response, you really should consider decoding it into a Pythondict
object.