python 有3種str 字符空行清除方法,分別為:
str.lstrip() # 清除左邊第一個
str.rstrip() # 清除右邊第一個
str.strip() # 清除兩邊
列:
s = "\nHello World\n "
print s.lstrip()
"Hello World\n"
print s.rstrip()
" \nHello World"
print s.strip()
"Hello World"
打開文件清除換行符
列:
hand = open('box.txt', 'r')
for line in hand:
line = line.rstrip() # or strip
print line