Python将txt文件内容转换成列表

很多时候,Python爬取的文件都是写在TXT文本里,不过当要提取出来进行数据分析时无法按行输入,
我在网上百度的方法不知道为啥有点不是我想要的结果,于是我换成另一种方式。
那就是先转成列表

方法一:

# -*- coding:utf-8 -*-
f = open(r'ip.txt','r')
a = list(f)
print(a)
f.close()

方法二:

# -*- coding:utf-8 -*-
f = open(r'ip.txt','r')
a=[i for i in f]
print(a)
f.close()

希望对大家有用

posted @ 2019-06-08 23:26  h云淡风轻  阅读(19)  评论(0编辑  收藏  举报  来源