python怎么去掉换行符“”?

python读取文件时去掉换行符“
”的方法如下:
import os
file='bsw.txt'
f=open(file,'r')
ff=f.readlines()
for line in ff:
line=line.rstrip("
")
print line
使用strip()函数去掉每行结束的
。
strip()函数原型:
声明:str为字符串,chars为要删除的字符序列
str.strip(chars):删除s字符串中开头、结尾处,位于chars删除序列的字符
str.lstrip(chars):删除s字符串中开头处,位于chars删除序列的字符
str.rstrip(chars):删除s字符串中结尾处,位于chars删除序列的字符
更多Python知识请关注云海天python教程网。
来源:PY学习网:原文地址:https://www.py.cn/article.html

