2016-09-06 66 views
1

我想根據新的行字符拆分字符串,並用'。'替換新行。在python中。我試過這個代碼,但我沒有得到它。如何從python中的字符串搜索新的行字符

import nltk 
from nltk.tokenize.punkt import PunktSentenceTokenizer, PunktParameters, PunktLanguageVars 
ex_sent="Conclusion & Future Work 
    In our project we have tried for implementing northbound SDN application for OpenFlow protocol evaluation. The client and the RYU application is being connected, via socket connection." 
class CommaPoint(PunktLanguageVars): 
    sent_end_chars = (',','\n') 
tokenizer = PunktSentenceTokenizer(lang_vars = CommaPoint()) 
sentences = sentence_splitter.tokenize(ex_sent) 
print sentences 

回答

4

換行符是\n。所以如果你想用.替換字符串中的所有換行符,你應該使用replace()方法。在這種情況下:

your_string.replace('\n', '.') 

的第二種方法是通過.split('\n')換行符然後'.'.join(your_string)。你應該結賬Python documentation.

+0

謝謝Laszlowaty我欠你一個... –