Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace '\n' with ' 'space in string using python

Tags:

python

I have a string of the below form:

"\nMy objective is to work with a progressive & innovative 
organisation where I can implement my skills, knowledge and \nachieve 
success.\n\nPHP\nSDE 2 \nQuikr\nZend\n(CommonFloor merged with 
Quikr)\nTasks\nTo develop Quikr Jobs portal\nLaravel\nTo develop 
intent-form to increase o\xef\xac\x84ine signups\nTo develop sales CRM 
tool\nJava\n \nSDE 1 J2EE\nCommonFloor\nTasks\nJava Spring\nTo work on 
hybrid mobile app for residential communities\nTo develop APIs for 
CommonFloor Groups\nJavaScript\n\nSDE 1 \njQuery\nIDrive India Pv
Ltd.\nTasks\nAngular JS\nTo develop APIs for cloud storage\nTo add  front-end functionalities for online backup and data sync\nGulp\n\nSDE 1 "

I want to replace '\n' with space using python. I tried this command:

doc=doc.replace('\n',' ')

but it's not replacing '\n' with space. How I can do this?

like image 755
neha pawar Avatar asked Apr 24 '17 10:04

neha pawar


2 Answers

Your string seems to be multiline so you need triple quotes '''. This code works:

doc = '''\nMy objective is to work with a progressive & innovative 
organisation where I can implement my skills, knowledge and \nachieve 
success.\n\nPHP\nSDE 2 \nQuikr\nZend\n(CommonFloor merged with 
Quikr)\nTasks\nTo develop Quikr Jobs portal\nLaravel\nTo develop 
intent-form to increase o\xef\xac\x84ine signups\nTo develop sales CRM 
tool\nJava\n \nSDE 1 J2EE\nCommonFloor\nTasks\nJava Spring\nTo work on 
hybrid mobile app for residential communities\nTo develop APIs for 
CommonFloor Groups\nJavaScript\n\nSDE 1 \njQuery\nIDrive India Pv
Ltd.\nTasks\nAngular JS\nTo develop APIs for cloud storage\nTo add  front-end functionalities for online backup and data sync\nGulp\n\nSDE 1'''
doc = doc.replace('\n', ' ')
like image 95
zipa Avatar answered Oct 15 '22 08:10

zipa


doc=''' your text '''
doc=doc.replace('\n',' ')
print doc
like image 20
Kenneth Nuthan Avatar answered Oct 15 '22 08:10

Kenneth Nuthan