2013-08-05 47 views

回答

0
# Checks if the text is more than 0 symbols. And if the last symbol is "\n" 
if len(test) > 0 and test[-1:]=="\n": 
    # If so, remove the last linebreak 
    test = test[0:-1] 
0

從文本文件中刪除換行符,您可以使用rstrip()可以:

with open('somefile.txt', 'r') as f: 
    for line in f: 
     print(line.rstrip()) 

要測試一個值是否斷行:

for item in some_values: 
    if item == '\n': 
     do something 

或者,你可以測試如果'\ n'在一行中:

with open('somefile.txt', 'r') as f: 
    for line in f: 
      if '\n' in line: 
       print(line.rstrip()) 
0

Unicode字符串有t與標準字符串相同的方法,可以使用line.replace(r'\ n','')刪除'\ n'並檢查它是否與'\ n'一起存在於unc

相關問題