def import_courses_from_file(file_name): with open(file_name, 'r',encoding='utf-8') as file: for line in file: data = line.strip().split(',') if len(data) >= 4: course_info.append({'课程编号': data[0], '课程名称': data[1],'课程学分': data[2],'上课时间':data[3]}) else: print("错误:行数据不完整,跳过该行") print(course_info) import_students_from_file(r"C:\Users\Lenovo\Desktop\文件导入学生信息.txt" json格式保存正确但信息导入失败,修改此代码
时间: 2024-04-20 22:23:34 浏览: 73
def import_courses_from_file(file_name):
course_info = []
with open(file_name, 'r', encoding='utf-8') as file:
for line in file:
data = line.strip().split(',')
if len(data) >= 4:
course_info.append({'课程编号': data[0], '课程名称': data[1], '课程学分': data[2], '上课时间': data[3]})
else:
print("错误:行数据不完整,跳过该行")
return course_info
course_info = import_courses_from_file(r"C:\Users\Lenovo\Desktop\文件导入学生信息.txt")
print(course_info)
相关问题
class Course: courses = [ Course('Advanced Mathematics', 4, 'Monday 8:30-10:10'), Course('College English', 3, 'Tuesday 2:30-4:10'), Course('Computer programming', 4, 'Wednesday 10:30-12:10'), Course('Computer Design', 3, 'Thursday 4:30-6:10') ] def __init__(self, name, credit, time): self.name = name self.credit = credit self.time = time self.courses = [] def add_course(self,course): self.courses.append(course) def print_courses(self): for course in self.courses: print(course.name) def choose_class(): print(''' 1.'Advanced Mathematics', 'credit:4', 'Monday 8:30-10:10')) 2.'College English', 'credit:3', 'Tuesday 2:30-4:10' 3.'Computer programming', 'credit:4', 'Wednesday 10:30-12:10' 4.'Computer Design', 'credit:3', 'Thursday 4:30-6:10''') ch = int(input('Please choose the class that you want:')) if ch == 1: Course.add_course(Course('Advanced Mathematics', 4, 'Monday 8:30-10:10')) elif ch == 2: Course.add_course(Course('College English', 3, 'Tuesday 2:30-4:10')) elif ch == 3: Course.add_course(Course('Computer programming', 4, 'Wednesday 10:30-12:10')) elif ch == 4: Course.add_course(Course('Computer Design', 3, 'Thursday 4:30-6:10')) Course.choose_class() Course.print_courses()
这段代码定义了一个课程类 `Course`,其中包含课程名称、学分和上课时间等属性,以及添加课程和打印已选课程的方法。其中 `courses` 列表包含了四门课程的信息。
在 `choose_class()` 函数中,通过打印课程信息列表,等待用户输入选择的课程,并根据用户的选择调用 `add_course()` 方法将该课程添加到已选课程列表中。
最后,调用 `print_courses()` 方法打印已选课程列表。但是,需要注意的是 `choose_class()` 函数中的最后一行 `Course.choose_class()` 应该是多余的,应该删除。
class Course: def __init__(self, name, credit, time): self.name = name self.credit = credit self.time = time self.courses = [] def add_course(self,course): self.courses.append(course) def print_courses(self): for course in self.courses: print(course.name) def choose_class(): print(''' 1.'Advanced Mathematics', 'credit:4', 'Monday 8:30-10:10')) 2.'College English', 'credit:3', 'Tuesday 2:30-4:10' 3.'Computer programming', 'credit:4', 'Wednesday 10:30-12:10' 4.'Computer Design', 'credit:3', 'Thursday 4:30-6:10''') ch = int(input('Please choose the class that you want:')) if ch == 1: Course.add_course(Course('Advanced Mathematics', 4, 'Monday 8:30-10:10')) elif ch == 2: Course.add_course(Course('College English', 3, 'Tuesday 2:30-4:10')) elif ch == 3: Course.add_course(Course('Computer programming', 4, 'Wednesday 10:30-12:10')) elif ch == 4: Course.add_course(Course('Computer Design', 3, 'Thursday 4:30-6:10'))
这段代码定义了一个课程类 `Course`,包含了课程名称、学分和上课时间等属性,以及添加课程和打印课程的方法。另外还定义了一个 `choose_class()` 函数,让用户选择课程并添加到课程列表中。
但是,这段代码存在几个问题:
1. `courses` 属性应该在类的外部定义,而不是在 `__init__` 方法中定义。否则每次实例化对象时都会创建一个新的空列表,而无法保留已经添加的课程。
2. 在 `choose_class()` 函数中,应该先创建一个 `Course` 对象,再将其添加到课程列表中。而不是直接调用 `add_course()` 方法,因为该方法需要先获取对应的 `Course` 对象。
3. 在 `choose_class()` 函数中,应该加上异常处理,防止用户输入不合法的选项。
修改后的代码如下:
```
class Course:
courses = []
def __init__(self, name, credit, time):
self.name = name
self.credit = credit
self.time = time
def add_course(self,course):
self.courses.append(course)
def print_courses(self):
for course in self.courses:
print(course.name)
def choose_class():
print('''1. 'Advanced Mathematics', 'credit:4', 'Monday 8:30-10:10'
2. 'College English', 'credit:3', 'Tuesday 2:30-4:10'
3. 'Computer programming', 'credit:4', 'Wednesday 10:30-12:10'
4. 'Computer Design', 'credit:3', 'Thursday 4:30-6:10' ''')
ch = input('Please choose the class that you want:')
try:
ch = int(ch)
if ch == 1:
course = Course('Advanced Mathematics', 4, 'Monday 8:30-10:10')
elif ch == 2:
course = Course('College English', 3, 'Tuesday 2:30-4:10')
elif ch == 3:
course = Course('Computer programming', 4, 'Wednesday 10:30-12:10')
elif ch == 4:
course = Course('Computer Design', 3, 'Thursday 4:30-6:10')
else:
raise ValueError
Course.courses.append(course)
except ValueError:
print("Invalid option")
choose_class()
```
阅读全文