文件内容如下:
Alex 100000 Rain 80000 Egon 50000 Yuan 30000 #此处有一个空行!
现在看如何处理并转成列表!
salary_info = open("salaryinfo.txt", "r+", encoding="UTF-8") salary_info_list = [] for line in salary_info.readlines(): if line == ‘\n‘: pass else: salary_info_list.append(list(line.split())) # 读到的列表以追加的方式追加到salary_list值中。
输出结果如下:
[[‘Alex‘, ‘100000‘], [‘Rain‘, ‘80000‘], [‘Egon‘, ‘50000‘], [‘Yuan‘, ‘30000‘]]
时间: 2024-10-19 20:15:54