美文网首页
将excel或者txt文件的每行转换成列表(list)

将excel或者txt文件的每行转换成列表(list)

作者: 大大的世界和小小的人儿 | 来源:发表于2021-07-07 11:33 被阅读0次

方法一:

with open('filename','r') as f:                    #读入文件
        L = [i.strip().split() for i in f]            #strip()函数,默认删除字符串头和尾的空白字符                                                           
        print(L)

也可以这样读入文件:

f= open(r'filename','r')                        #读入文件
L = [i.strip().split() for i in f]             
print(L)
f.close()

相关文章

网友评论

      本文标题:将excel或者txt文件的每行转换成列表(list)

      本文链接:https://www.haomeiwen.com/subject/vrozultx.html