使用replace和strip函数删除open()函数独处的回车问题
在linux系统中通常会读某一个文件的每一行,然后拼成字符串.使用shell如下
#!/bin/bash
for i in `cat a.txt`;do
echo "aaa$ibbb"
done 结果如下:
aaatestbbb 但是看python执行的结果
#!/usr/bin/python
#-*- coding: utf-8 -*-
m=open('a.txt','r')
for i in m:
print("aaa%sbbbb" % i) 结果如下:
aaatest
bbb 在这里隐藏了一个\n
解决方案1,使用replace函数
#!/usr/bin/python
#-*- coding: utf-8 -*-m=open('a.txt','r'):
for i in m:
m=i.replace('\n','')
print("aaa%sbbbb" % i) 解决方案2,使用strip函数
#!/usr/bin/python
#-*- coding: utf-8 -*-
m=open('a.txt','r')
for i in m:
m=i.strip()
print("aaa%sbbbb" % i) 结果如下:
aaatestbbb
页:
[1]