音乐分享(自动连播):
So cold
Not my baby
Wicked Game
What Is Love
Once Upon a Time
The Heart Of The Ocean
Sun Goes Down
Move That Body
Lose Control
云山
一趟
记录分享 记录是一种习惯 、分享是一种态度
python中去掉readline() 的换行符
· 技术积累 · Python

python使用readline()时读出的内容把换行(\n)也带上了,这并不是我们想要的结果。

示例:

#文本 test.txt
111111
222222
333333
444444
555555
666666

#脚本 readtest.py
#!/usr/bin/python
f = open('test.txt','r')
for i in range(5):
    print(f.readline())

#执行脚本逐行读取结果
[root@localhost ~]# python readtest.py 
111111

222222

333333

444444

555555

去掉换行符:

#第一种
print(f.readline().strip())

#第二种
print(f.readline().split('\n')[0])

#第三种
print(f.readline().replace('\n',''))


#脚本执行的结果
[root@localhost ~]# python readtest.py 
111111
222222
333333
444444
555555

本文最后更新时间 2019-02-25
文章链接地址:https://me.jinchuang.org/archives/405.html
本文内容未来会持续更新·本站文章除注明[转载|引用|原文]出处外,均为本站原生内容,转载前请注明出处


留言列表

  1. cindy
    cindy Windows 10 Microsoft Edge · 天津市天津市 · 回复

    看到代码就头大。。。

    1. J.C
      J.C Windows 10 Google Chrome · 上海市上海市 · 回复

      脑瓜子疼?‍♀️

留言

顶部