使用python读取文本时,发现输出时,会有空行,经百度找到解决方案 


文本1内容如下:

1
2
3
4
5
6
67

python内容如下:

#!/usr/bin/env python
# _*_coding:utf-8_*_
file = open('1','r')
for i in file:
    print(i)

读取后的效果如下:

image.png


解决方案  使用strip 函数

改进后如下:

#!/usr/bin/env python
# _*_coding:utf-8_*_
file = open('1','r')
for i in file:
    i=i.strip()
    print(i)


效果如下:

image.png


描述:

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。

语法

strip()方法语法:

str.strip([chars]);