Rejo Varghese Rejo Varghese - 2 months ago 11
Python Question

how to search new line character from a string in python

I want to split string based on new line character and replace the new line with a '.' in python. I tried this code but I'm not getting it.

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

Answer

The newline character is \n. So If you want to replace all new line characters in string with . you should use replace() method. In this case:

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

A second approach is to .split('\n') by newline and then '.'.join(your_string). You should checkout the Python documentation.