How can I make python be more friendly regarding reading and writing Unicode text files? -


I have found that even modern Python version (such as 3.x) detects BOM on text files Not able to. I would want to know whether there is a module that Place the can open add this feature missing Python () are and codecs.open () read more Work files to write text

The suggestion of the solution still sounds good for me (here is the modified version of that code, however example of the Python 2, Python 3, and using a):

  # / usr / bin / python # - * - coding: utf-8 - * - import Codex, Access, cis logging .basicConfig (level = logging.INFO) bomdict = {codecs.BOM_UTF8: 'UTF8', codecs.BOM_UTF16_BE: 'name of UTF-16BE', codecs.BOM_UTF16_LE: 'UTF-16LE'} def read_unicode (file ): The_text = open (file name, 'r'). Read for Baum (), Bombed in encoding Items (): If The_textkstartswith (Bom): logging.info ( 'got BOM,% using S, encoding) the_text = the_text [len (Bohm):] Break: logging.info (' No, BOM, UTF-8 ') encoding = "UTF-8" Return t He_text.decode (encoding) f = open (' x.txt ',' wb ') f.write (codecs.BOM_UTF16_LE) f.write (u'zeé fóo!' . encode ( 'UTF-16LE')) F Close () Print Reed_inikod ( 'x.txt')  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -