title: Character Encoding From Chinese to Latin1 in Python
body: <p>I'm trying to convert a localization file that contains Chinese characters such that the chinese characters are converted into latin1 encoding.</p>
  
  <p>However, when I run the python script I get this error...</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xb9 in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>Here's my python script, it essentially just takes the users input to convert said file. Then converts the file (all lines that start with a [ or are empty, should be skipped)... The part that needs to be converted is always at index 1 in an list.</p>
  
  <pre><code># coding: utf8
  
  # Enter File Name
  file_name = raw_input('Enter File Path/Name To Convert: ')
  
  # Open the File we Write too...
  write_file = open(file_name + "_temp", 'w+')
  
  # Open the File we Read From...
  read_file = open(file_name)
  
  with open(file_name) as file_to_write:
      for line in file_to_write:
          # We ignore any line that starts with [] or is empty...
          if line and line[0:1] != '[':
              split_string = line.split("=")
              if len(split_string) == 2:
                  write_file.write(split_string[0] + "=" + split_string[1].encode('gbk').decode('latin1') + "\n")
              else:
                  write_file.write(line)
          else:
              write_file.write(line)
  
  
  
  # Close File we Write too..
  write_file.close()
  
  # Close File we read too..
  read_file.close()
  </code></pre>
  
  <p>And example config file is...</p>
  
  <pre><code>[Example]
  Password=密碼
  </code></pre>
  
  <p>The output should be converted into...</p>
  
  <pre><code>[Example]
  Password=±K½X
  </code></pre>