Python: Convert File Encoding

By Xah Lee. Date: . Last updated: .

Here's a script to convert a file encoding.

# -*- coding: utf-8 -*-
# python 2

path='infile.html'
path2='outfile.html'

f= open(path, 'rb')
content= unicode(f.read(), 'gb18030')
f.close()
f= open(path2, 'wb')
f.write(content.encode('utf-8'))
f.close()

(thanks to Andrew Clover for help.)

File Encoding

  1. Unicode Basics: Character Set, Encoding, UTF-8, Codepoint
  2. HTML: Character Sets and Encoding
  3. Unicode in Ruby, Perl, Python, JavaScript, Java, Emacs Lisp, Mathematica
  4. Python: Unicode Tutorial 🐍
  5. Python: Convert File Encoding
  6. Python: Convert File Encoding for All Files in a Dir
  7. Perl: Unicode Tutorial 🐪
  8. Perl: Convert File Encoding
  9. Ruby: Unicode Tutorial 💎
  10. Java: Convert File Encoding
  11. Linux: Convert File Encoding with iconv

Python Text Processing

  1. Read/Write File
  2. Walk Directory
  3. Python 3: Walk Directory
  4. File Path
  5. Process Unicode
  6. Convert File Encoding
  7. Convert File Encoding in a Dir
  8. Find Replace in dir
  9. Find Replace by Regex
  10. Count Word Frequency

If you have a question, put $5 at patreon and message me.

Python

  1. Python 3 Basics
  2. Python 2 Basics
  3. Python 2 and 3 Difference
  4. Print Version
  5. Builtin Help
  6. Quote String
  7. String Methods
  8. Format String
  9. Operators
  10. True, False
  11. if then else
  12. Loop
  13. List Basics
  14. Loop Thru List
  15. Map f to List
  16. List Comprehension
  17. List Methods
  18. Sort
  19. Dictionary
  20. Loop Thru Dict
  21. Dict Methods
  22. Function
  23. Class
  24. Object, ID, Type
  25. List Modules
  26. Write a Module
  27. Unicode 🐍

Regex

  1. Regex Basics
  2. Regex Reference

Text Processing

  1. Read/Write File
  2. Traverse Directory
  3. 2 Traverse Directory
  4. File Path
  5. Process Unicode
  6. Convert File Encoding
  7. Find Replace in dir
  8. Find Replace by Regex
  9. Count Word Frequency

Web

  1. Send Email
  2. GET Web Page
  3. Web Crawler
  4. HTTP POST

Misc

  1. JSON
  2. Find Script Path
  3. Get Env Var
  4. System Call
  5. Decompress Gzip
  6. Complex Numbers
  7. Copy Nested List
  8. Tuple vs List
  9. Sets, Union, Intersection
  10. Closure
  11. 2 Closure
  12. Decorator
  13. Append String in Loop
  14. Timing f timeit
  15. Keyword Arg Default Value Unstable
  16. Check Page Load Size
  17. Thumbnail Generation