python 空行清除


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM