Python脚本合并求助:空调设备读写脚本整合需求
Hey there! Welcome to your first post—super stoked you're building scripts for your AC device while learning Python, that's such a hands-on way to grow your skills. Let's walk through merging those two scripts and handling that text extraction together.
First, let's map out the core flow your combined script should follow:
- Read raw data from the AC device
- Parse that long raw text to pull out only the specific info you need
- Send data (either the extracted info or commands based on it) back to the AC
Here's a basic skeleton you can adapt with your existing code:
# Import whatever modules your original scripts use (serial, requests, etc.) import [your_required_modules] def read_ac_data(): # Paste the full logic from your existing "read data" script here # This function should return the raw long text from the AC raw_data = "" # Example (if using serial): # ser = serial.Serial('/dev/ttyUSB0', 9600) # raw_data = ser.readline().decode('utf-8') # ser.close() return raw_data def extract_target_info(raw_text): # This is where we'll slice and dice the long text to get what you need # Customize this entirely based on how your AC's raw data is formatted extracted = {} # Example 1: If raw data has line-separated values like "Temperature: 24°C" for line in raw_text.split('\n'): line = line.strip() if line.startswith("Temperature:"): extracted["temperature"] = line.split(":")[1].strip() elif line.startswith("Mode:"): extracted["mode"] = line.split(":")[1].strip() # Example 2: If raw data is a messy string, use regex to find patterns # import re # temp_match = re.search(r"Temp:\s*(\d+)", raw_text) # if temp_match: # extracted["temperature"] = temp_match.group(1) return extracted def send_ac_data(processed_data): # Paste the logic from your existing "send data" script here # Use the extracted info to build the data/commands you need to send try: # Example send logic (adjust to your device's protocol): # ser = serial.Serial('/dev/ttyUSB0', 9600) # ser.write(f"SET_TEMP:{processed_data['temperature']}\n".encode('utf-8')) # ser.close() print(f"Successfully sent data: {processed_data}") except Exception as e: print(f"Oops, failed to send data: {str(e)}") def main(): # Step 1: Grab raw data from AC raw_ac_data = read_ac_data() if not raw_ac_data: print("Error: Couldn't read data from the AC device") return # Step 2: Pull out the info you care about target_info = extract_target_info(raw_ac_data) if not target_info: print("Error: No relevant info found in the raw data") return print(f"Extracted AC Data: {target_info}") # Step 3: Send data (modify this if you need to adjust the data before sending) send_ac_data(target_info) if __name__ == "__main__": main()
The biggest piece you'll need to tweak is the extract_target_info function. Since I don't know exactly what your AC's raw output looks like, here are quick tips:
- If the raw text is structured (like JSON or XML), use Python's built-in modules (
json,xml.etree.ElementTree) to parse it cleanly - If it's unstructured but has consistent patterns, use string methods (
split,strip,startswith) or regular expressions (remodule) to hunt for specific values - If you can share a sample of the raw text, I can help write the exact extraction code!
Take the code from your original "read" script and drop it into the read_ac_data function—make sure it returns the raw long text. Do the same for your "send" script in send_ac_data, and adjust it to accept the extracted info as input (so you can send commands based on what you read).
Since you're still building your foundation, adding basic error checks (like the ones in the skeleton) will help you debug when things go wrong—no more mysterious crashes!
Feel free to share snippets of your original scripts or a sample of the raw AC data if you get stuck on any part. Happy coding!
内容的提问来源于stack exchange,提问作者akkaria

