title: How to get rid of \n and \r in a string using python
body: <p>I have written a python(2.7) program to retreive data from a table in a database and copy it into a csv file. There are various data in non-printable format(unicode) which contain \n, \r. Because of \n, \r I am not able to retreive the data as it is in the table.</p>
  
  <p>I have tried the following</p>
  
  <pre><code>str.replace('\n','').replace('\r',' ')
  str.replace('\n','\\n').replace('\r', '\\r')
  </code></pre>
  
  <p>but it did not work out</p>
  
  <p>csv code</p>
  
  <pre><code> cur.execute('select * from db.table_name)
  with open('test.csv','w') as csv_file:
      csv_writer=csv.writer(csv_file)
      for row in cur:
          print "row = ", count
          count = count + 1
          newrow=[];
          for index in range(0, len(row)):
              value= row[index]
              if(type(row[index])is str):
                   value=row[index].replace("\n"," ").replace("\r"," ")
              newrow.append(value)
         csv_writer.writerow(newrow)
  </code></pre>