Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- from datetime import datetime
- # File paths
- student_file = "studentinfo.csv"
- input_file = "input.txt"
- # Generate timestamp
- timestamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
- output_file = f"BTP_MTP_2027_output_{timestamp}.csv"
- # Read studentinfo into dictionary (case-insensitive roll mapping)
- students = {}
- with open(student_file, newline='', encoding='utf-8') as f:
- reader = csv.DictReader(f)
- for row in reader:
- roll = row.get("Roll No", "").strip().lower()
- students[roll] = {
- "name": row.get("Name", "").strip(),
- "roll": row.get("Roll No", "").strip(),
- "email": row.get("email", "").strip(),
- "aemail": row.get("aemail", "").strip(),
- "contact": row.get("contact", "").strip()
- }
- # Read input rolls
- with open(input_file, 'r', encoding='utf-8') as f:
- input_rolls = [line.strip() for line in f if line.strip()]
- # Write output
- with open(output_file, 'w', newline='', encoding='utf-8') as f:
- fieldnames = ["Name", "Roll No", "email", "aemail", "contact"]
- writer = csv.DictWriter(f, fieldnames=fieldnames)
- writer.writeheader()
- for roll in input_rolls:
- key = roll.lower()
- if key in students:
- s = students[key]
- formatted_name = f"xIITP {s['name']} {s['roll'].upper()}"
- writer.writerow({
- "Name": formatted_name.strip(),
- "Roll No": s["roll"],
- "email": s["email"],
- "aemail": s["aemail"],
- "contact": s["contact"]
- })
- else:
- writer.writerow({
- "Name": "na",
- "Roll No": roll.strip(),
- "email": "na",
- "aemail": "na",
- "contact": "na"
- })
- print(f"Output file created: {output_file}")
Advertisement
Add Comment
Please, Sign In to add comment