Python新手求助:合并两个TXT文件并按指定格式输出员工参会信息
Hey there! Let's work through integrating your code to meet your exact requirements. Since you're new to Python, I'll break this down step by step so it's easy to follow.
First, Let's Clarify What We Need to Do
Your main goal is to:
- Read employee attendance data from
employee.txt - Calculate two extra fields: whether an employee attended both days or neither day
- Output everything in the format:
姓氏, 名字, Day1, Day2, 两天都参加, 都不参加 - Also process and display the content from
confPack.txt
The Fix: Integrated Working Code
I've rewritten and merged your code into a complete script that handles all these tasks. Here's the full code:
# First, show content from confPack.txt print("📋 Conf Pack Messages:") with open("confPack.txt", "r") as conf_file: for line_num, line in enumerate(conf_file, 1): # Clean up trailing newlines and format output print(f"Line {line_num}: {line.strip()}") # Add a separator for readability print("\n---\n") # Now process employee attendance data print("👥 Employee Attendance Summary:") # Print the header first print("姓氏, 名字, Day1, Day2, 两天都参加, 都不参加") with open("employee.txt", "r") as emp_file: for line in emp_file: # Split the line into parts using commas, and remove extra whitespace/newlines employee_data = line.strip().split(',') # Extract each field (handle cases where there might be trailing commas) last_name = employee_data[0].strip() first_name = employee_data[1].strip() day1_raw = employee_data[2].strip() if len(employee_data) >= 3 else "" day2_raw = employee_data[3].strip() if len(employee_data) >= 4 else "" # Normalize attendance status: turn empty values into "N" (not attending) day1_status = "Y" if day1_raw == "Y" else "N" day2_status = "Y" if day2_raw == "Y" else "N" # Calculate the two extra fields both_attended = "Y" if day1_status == "Y" and day2_status == "Y" else "N" neither_attended = "Y" if day1_status == "N" and day2_status == "N" else "N" # Print the final formatted line print(f"{last_name}, {first_name}, {day1_status}, {day2_status}, {both_attended}, {neither_attended}")
Let's Explain What This Code Does
Processing
confPack.txt:- We open the file and loop through each line, printing it with a clear label so you can see the content clearly.
- The
strip()method removes any extra newlines or spaces from each line.
Processing
employee.txt:- We split each line by commas to get individual employee details. The
strip()method cleans up any accidental spaces or trailing commas (like in your example lines). - We normalize the attendance status: if the field is empty, we treat it as "N" (not attending) to make our logic simpler.
- We calculate the two extra fields using basic boolean checks:
both_attendedis "Y" only if both Day1 and Day2 are "Y"neither_attendedis "Y" only if both Day1 and Day2 are "N"
- Finally, we print everything in the exact format you requested.
- We split each line by commas to get individual employee details. The
Why Your Original Code Had Issues
- The
str = Y[0]line was invalid (you didn't defineYfirst) — we don't need that loop at all, since we just need to check if each day's status is "Y" or not. - You weren't linking the employee data processing to the conf pack processing — this script combines both tasks smoothly.
How to Run It
- Make sure
employee.txt,confPack.txt, and this Python script are saved in the same folder. - Run the script using Python, and you'll see the conf pack messages followed by the formatted employee attendance summary.
内容的提问来源于stack exchange,提问作者Nena Carreira
相关产品推荐
相关产品推荐

