title: Python replacing literal \n
body: <p>I didn't want to ask here because I eventually got this to work, but I don't understand why my other attempts were not viable. It is driving me insane. I looked up so many forums.</p>
  
  <p>Currently I have</p>
  
  <pre><code>filedata.replace('\\n','')
  </code></pre>
  
  <p>which removed literal <code>\n</code>, followed by </p>
  
  <pre><code>filedata = re.sub("': ;\d{5,6},\n'",', ',filedata)
  </code></pre>
  
  <p>to remove the subsequent "crap" in my output file</p>
  
  <p>I could not, for the life of me, put this into one command. I don't know what I am missing, but this is because I cannot escape <code>\n</code> in <code>re.sub</code> (<code>\\n</code> does not work). And, I cannot escape <code>,</code> in <code>filedata.replace</code>; <code>\,</code> did not work, but <code>\'</code> works to escape <code>'</code>. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Input:</p>
  
  <pre><code>Foo\nBarFoo,
  Foo\nBarFoo
  </code></pre>
  
  <p>Desired Output:</p>
  
  <pre><code>FooBarFoo, FooBarFoo
  </code></pre>
  
  <p>This works:</p>
  
  <pre><code>filedata = filedata.replace('\\n','')
  filedata = re.sub(",\n",', ',filedata)
  </code></pre>
  
  <p>so the first line replaces <code>literal \n</code> and second line replaces <code>literal ,</code> and newline (<code>\n</code>). What I could not do is get <code>.replace</code> to escape <code>,</code> (<code>\,</code>, etc. did not work) nor can I escape <code>\n</code> (newline) in <code>.sub</code> (<code>\\n</code>, etc. did not work). So I had to do it in two different steps, while I would prefer to do both in one.</p>
  
  <p>I understand there are other ways to just remove characters if I am not actually replacing, this is just a simplified example of what I am doing. Thanks.</p>