title: Parsing through newline characters in Python
body: <p>I am working on a fairly basic encoder/decoder where you can input your own text file (as a string) and your own encoder (also as a string: it must be a text file).</p>
  
  <p>Here is my decoder function:</p>
  
  <pre><code>def cDecode(file_name, encoder='standard_encoder.txt', save_new=True): # does not decode multi-lines correctly -- everything goes on a single line. See next comment
      '''Decodes &lt;'file_name'&gt; with the reverse method of &lt;'encoder'&gt;.'''
      if type(file_name) != str or type(encoder) != str: raise TypeError("&lt;'file_name'&gt; and &lt;'encoder'&gt; must be of type &lt;'str'&gt;.")
      if type(save_new) != bool: raise TypeError("&lt;'save_new'&gt; must be of type &lt;'bool'&gt;.")
      if file_name[-4:] != '.txt': file_name += '.txt'
      if encoder[-4:] != '.txt': encoder += '.txt'
      decoder_set = {}
      try:
          with open(encoder, 'r') as encoding_file:
              for line in encoding_file:
                  line_parts = line.split(': ')
                  my_key, my_value = line_parts[1], line_parts[0]
  </code></pre>
  
  <p>I think the error is in here:
  I have to remove the '\n' because every character (in the decoding file) is on a new line, like such: 'A: Ð'.</p>
  
  <pre><code>                if '\n' in my_key:
                          loc = my_key.find('\n') # this may be the cause of the single-line of the decoding.
                          my_key = my_key[:loc] + my_key[loc + 1:]
                  decoder_set[my_key] = my_value
          encoding_file.close()
      except IOError:
          encoder = 'standard_encoder.txt'
          with open(encoder, 'r') as encoding_file:
              for line in encoding_file:
                  line_parts = line.split(': ')
                  my_key, my_value = line_parts[1], line_parts[0]
                  # every key has a new line character automatically because it's on a different line
                  if '\n' in my_key:
                          loc = my_key.find('\n')
                          my_key = my_key[:loc] + my_key[loc + 1:]
                  decoder_set[my_key] = my_value
          encoding_file.close()
      decodingKeys = decoder_set.keys()
  </code></pre>
  
  <p>Here is the rest of the function:</p>
  
  <pre><code>    if save_new:
          try:
              decoded_file_name = file_name[:-12] + '_decoded' + file_name[-4:]
              encoded_file = open(decoded_file_name, 'a+')
              with open(file_name, 'r') as my_file:
                  for line in my_file:
                      de_line = ''
                      for char in line:
                          if char in decodingKeys: de_char = decoder_set[char]
                          else: de_char = char
                          de_line += de_char
                      encoded_file.write(de_line)
          except IOError:
              raise NameError(file_name + ' was not found. Decoding process terminated.')
      else:
          try:
              import os
              encoded_file = file_name[:-12] + '_decoded' + file_name[-4:]
              with open(file_name, 'r+') as my_file:
                  for line in my_file:
                      de_line = ''
                      for char in line:
                          if char in decodingKeys: en_char = decoding_set[char]
                          else: de_char = char
                          de_line += de_char
                      encoded_file.write(de_line)
                  os.remove(file_name)
                  os.rename(encoded_file, file_name)
          except IOError:
              raise NameError(file_name + ' was not found. Decoding process terminated.')
  </code></pre>
  
  <p>Say I have a multi-line text-file: </p>
  
  <pre><code>This is a test.
  As is this one.
  Good bye!
  </code></pre>
  
  <p>When encoded and then decoded afterward, it shows up like this: <code>This is a test.As is this one.Good bye!</code>.</p>
  
  <p>How can I fix this? I'm expecting it to show up like:</p>
  
  <pre><code>This is a test.
  As is this one.
  Good bye!
  </code></pre>
  
  <p>Thanks!</p>