解决python文件字符串转列表时遇到空行的问题
文件内容如下:
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']]
相关推荐
坚持是一种品质 2020-11-16
zhaobig 2020-11-16
文山羊 2020-10-31
kikaylee 2020-10-31
oXiaoChong 2020-10-30
PYTandFA 2020-09-15
CloudStrife0 2020-08-23
hang0 2020-08-16
lzujiangge 2020-08-16
RocNg 2020-08-15
xiesheng 2020-08-06
Tristahong 2020-08-05
georgeandgeorge 2020-08-03
elizabethxxy 2020-07-27
flycony 2020-07-05
柠檬班 2020-07-04
magicandy 2020-07-04
fanhuasijin 2020-06-27