如何实现用户输入信息与TXT文件数据匹配对比及副作用输出
Hey there! Let's break this down step by step—no need to overcomplicate things since you just need the basics to get started. I'll walk you through reading a TXT file, parsing its data, and building the matching logic you need.
First, let's assume your TXT file uses a clear separator (like |) to split each field per line. For example:
Alice|Female|32|Lisinopril|Hypertension|Dizziness, Dry cough
Bob|Male|45|Metformin|Type 2 Diabetes|Nausea, Fatigue
Charlie|Male|38|Lisinopril|Hypertension|Headache
If your file uses commas, tabs, or another separator, just adjust the parsing code later—this is just a common, easy-to-use format.
We'll use Python for this (it's the most beginner-friendly language for quick scripting like this). Here's a simple function to read the file and turn each line into a structured dictionary:
def read_medical_records(file_path): records = [] # Use 'with open' to safely handle file operations (auto-closes the file) with open(file_path, 'r', encoding='utf-8') as f: # Uncomment the line below if your TXT has a header row (e.g., "Name|Gender|Age...") # next(f) for line in f: # Remove extra newlines and split the line by your separator parts = line.strip().split('|') # Convert each line into a dictionary for easy access later record = { 'name': parts[0], 'gender': parts[1], 'age': int(parts[2]), # Convert age to integer for numerical comparison 'medication': parts[3], 'condition': parts[4], 'side_effects': parts[5] } records.append(record) return records
This function takes your TXT file path and returns a list of dictionaries—each dictionary represents one person's record, with clear labels for each piece of data.
Next, we'll get the user's info using simple input prompts:
# Get user's details user_name = input("Enter your name: ") user_gender = input("Enter your gender (e.g., Male/Female): ") user_age = int(input("Enter your age: ")) user_medication = input("Enter your medication: ") user_condition = input("Enter your medical condition: ")
Now we'll loop through the records we read, first matching the condition, then checking if age, gender, or medication aligns. We'll collect any matching side effects (and avoid duplicates):
# Load records from your TXT file (replace with your actual file path) records = read_medical_records('medical_records.txt') # Store unique side effects from matching records matched_side_effects = [] for record in records: # First: Exact match on condition (case-insensitive to avoid typos) if record['condition'].lower() == user_condition.lower(): # Second: Check if age, gender, or medication matches (adjust logic if you need "and" instead of "or") age_match = abs(record['age'] - user_age) <= 5 # Allow ±5 year age range (more flexible than exact match) gender_match = record['gender'].lower() == user_gender.lower() med_match = record['medication'].lower() == user_medication.lower() if age_match or gender_match or med_match: # Split side effects and add to our list (no duplicates) effects = record['side_effects'].split(', ') for effect in effects: if effect not in matched_side_effects: matched_side_effects.append(effect) # Print the results if matched_side_effects: print(f"Based on matching records, potential side effects to note:") for effect in matched_side_effects: print(f"- {effect}") else: print("No matching records found—no side effect references available.")
- Safe File Handling: Using
with open()ensures the file is closed automatically, so you don't have to worry about resource leaks. - Case Insensitivity: Converting text to lowercase (
.lower()) prevents mismatches from things like "Hypertension" vs "hypertension". - Flexible Age Matching: Using an age range (±5 years) is more practical than requiring an exact age match—you can tweak this number or switch to exact equality if needed.
- Duplicate Prevention: We only add each side effect once, even if it appears in multiple records.
Save the sample TXT content I listed earlier as medical_records.txt, run the script, and input something like:
Enter your name: David
Enter your gender: Male
Enter your age: 35
Enter your medication: Lisinopril
Enter your medical condition: Hypertension
You'll get output like:
Based on matching records, potential side effects to note:
- Dizziness
- Dry cough
- Headache
If your TXT uses a different separator (like commas), just replace split('|') with split(',') and adjust the parsing accordingly.
内容的提问来源于stack exchange,提问作者Jackson Wicktora




