替换' \ n'用' '使用python在字符串中的空格

时间:2017-04-24 10:38:40

标签: python

我有一个以下形式的字符串:

"\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 "

我想使用python将'\n'替换为space。我试过这个命令:

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

但它没有取代' \ n'与空间。我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您的字符串似乎是多行的,因此您需要三引号'''。此代码有效:

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', ' ')

答案 2 :(得分:0)

另一种方式是:

results: {
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "dishSuggester": [
    {
      "text": "potato chips with strawberry jam",
      "offset": 0,
      "length": 49,
      "options": [] # Should be some results here :-(
    }
  ]
}