读取txt文档中的每一行
#-*- coding:UTF-8 -*-
#读取字符串
if __name__=='__main__':
sp = open('test.txt','r')
for i in sp:
print i
运行结果:
>>>
www.baidu.com
www.sina.com
www.2233.com
www.12354.com
每行输出会把换行符也输出,所以中间会空一行
去掉“\n”
#-*- coding:UTF-8 -*-
#读取字符串,去除字符串中的“\n”
if __name__=='__main__':
sp = open('test.txt','r')
for i in sp:
if "\n" in i:
t = i.replace('\n','') #替换掉\n
print t
运行结果:
>>>
www.baidu.com
www.sina.com
www.2233.com
www.12354.com