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
本文最后记录时间 2024-03-31
文章链接地址:https://wojc.cn/archives/405.html
本站文章除注明[转载|引用|来源|来自],均为本站原创内容,转载前请注明出处
文章链接地址:https://wojc.cn/archives/405.html
本站文章除注明[转载|引用|来源|来自],均为本站原创内容,转载前请注明出处
看到代码就头大。。。
脑瓜子疼?♀️