''' student_list = [['张三','22','110'],['李四','22','110'],['王五','22','110']] ''' # 存储 student_list = [['张三','22','110'],['李四','22','110'],['王五','22','110']] # 1. 打开文件 file_handle = open('student_v2.txt','w') # 2. 写入数据 for student in student_list: pass # 3.关闭文件 file_handle.close() # 取出 file_handle = open('student_v2.txt',mode='r') contents = file_handle.readlines() # student_list = [['张三','22','110'],['李四','22','110'],['王五','22','110']] student_list = [] for msg in contents: msg = msg.strip('\n') # split() 通过某个字符分割字符串,返回的是分割完成后的列表 list_1 = msg.split(' ') # pop()函数 移除列表中最后一个数据 list_1.pop() # 把小列表添加到大列表中 student_list.append(list_1) file_handle.close() print(student_list)
python存储多条文件
最新推荐文章于 2024-03-29 17:33:53 发布