10

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?

6
  • 2
    In my world it works. Also if it's multyline string consider to use """text""" Commented Apr 24, 2017 at 10:40
  • 1
    What do you mean "not working"? That is not an adequate problem specification. Commented Apr 24, 2017 at 10:40
  • And what is the error?? or what is the output instead? Commented Apr 24, 2017 at 10:44
  • 4
    did you try str.replace? str.replace("\n", " ") should do the trick Commented Apr 24, 2017 at 10:44
  • 1
    .replace('\n',' ') should work Commented Apr 24, 2017 at 10:52

3 Answers 3

15

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', ' ')
Sign up to request clarification or add additional context in comments.

Comments

10
doc=''' your text '''
doc=doc.replace('\n',' ')
print doc

2 Comments

you should provide a text with your answer, not just pure code. And to me your code does not solve the problem as there is no new line which could be replaced and it wouldn't be replaced with a space (' ').
ok, thank you, this is my first time, I'll see to it from next. It should have been a (' ') instead of ('') I missed that.
4

Another way is:

doc = doc.split('\n')
doc = " ".join(doc)
print doc

1 Comment

above logic will work ok " sting ..." also, no need to be convert to """ / ''' .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.