How do i open a file encoding in python?
I have a Python codebase, built for Python 3, which uses Python 3 style open() with encoding parameter: Show https://github.com/miohtama/vvv/blob/master/vvv/textlineplugin.py#L47
Now I'd like to backport this code to Python 2.x, so that I would have a codebase which works with Python 2 and Python 3. What's the recommended strategy to work around Could I have a Python 3
TuringTux 5291 gold badge12 silver badges24 bronze badges asked Jun 10, 2012 at 18:03
Mikko OhtamaaMikko Ohtamaa 78.8k47 gold badges238 silver badges396 bronze badges 1. To get an encoding parameter in Python 2:If you only need to support Python 2.6 and 2.7 you can use
If you need speed, and you need to support Python 2.6 or earlier, you can use
2. To get a Python 3 open() style file handler which streams bytestrings:
Note the 'b', meaning 'binary'.
answered Jun 11, 2012 at 6:32
Lennart RegebroLennart Regebro 161k41 gold badges221 silver badges251 bronze badges 7 I think
should do. answered Jun 10, 2012 at 18:04
3 Here's one way:
Here's how to do the same thing when writing:
answered Nov 17, 2016 at 15:38
FlimmFlimm 122k39 gold badges233 silver badges245 bronze badges 4 This may do the trick:
Then you can keep you code in the python3 way. Note that some APIs like answered Nov 21, 2014 at 10:13
user2395922user2395922 8901 gold badge9 silver badges8 bronze badges 1 If you are using
And, Python 2 support abandon is just deleting everything related to answered Jun 10, 2019 at 4:51
YaOzIYaOzI 13.9k6 gold badges69 silver badges68 bronze badges Not a general answer, but may be useful for the specific case where you are happy with the default python 2 encoding, but want to specify utf-8 for python 3:
answered Aug 4, 2020 at 14:00
MarkHMarkH 4194 silver badges5 bronze badges How do I open an encoded file in Python?This mode is used for all files that don't contain text (e.g. images). Because read mode 'r' and text mode 't' are default modes, you do not need to specify them.
...
Specify File Mode.. How do I open a UTFUse open() to open a file with UTF-8 encoding
Call open(file, encoding=None) with encoding as "UTF-8" to open file with UTF-8 encoding.
How do I open a UTF file?How to Open UTF-8 in Excel. Launch Excel and select "Open Other Workbooks" from the opening screen. ... . Select "Computer," and then click "Browse." Navigate to the location of the UTF file, and then change the file type option to "All Files.". Select the UTF file, and then click "Open" to launch the Text Import Wizard.. |