>  Q&A  >  본문

Python의 지침 및 명령줄 작업

Python으로 스크립트를 작성 중인데 몇 가지 문제가 있습니다. 데이터베이스에서 가져온 매개변수를 사용하여 명령을 실행해야 합니다. 다음을 수행합니다(mysql.connector를 설치해야 함).

으아악

3가지 질문이 있습니다:

  1. 하위 프로세스 대신 os를 사용하는 것이 맞나요?
  2. 이 명령을 실행하면 json 형식으로 응답을 받게 되는데, 그 값을 어떻게 얻을 수 있나요? "import json"을 포함해야 합니까?
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])

P粉378890106P粉378890106408일 전464

모든 응답(1)나는 대답할 것이다

  • P粉194541072

    P粉1945410722023-09-09 10:31:16

    이유를 알고 싶다면 subprocess,请阅读这个를 사용해야 합니다.


    Python에서 import <module>的用法与C/C++#include <module>전처리 지시문은 약간의 차이점이 있지만 매우 유사합니다. 따라서 include import json은 필요하지 않지만 import json은 필요합니다.

    어쨌든 json是Python内置模块,用于解析、编码、缩进和写入.json文件,如果你得到一个JSON格式的响应,你真的应该考虑将其解码为Python的dict이의가 있습니다.

    회신하다
    0
  • 취소회신하다