wiki:python

Python

python tips

  • 자동으로 file encoding 확인하기 (BOM도..)
    bytes = min(32, os.path.getsize(filename));
    raw = open(filename, 'rb').read(bytes);
    if raw.startswith(codecs.BOM_UTF8):
    	encoding = 'utf-8-sig'
    else:
    	result = chardet.detect(raw);
    	encoding = result['encoding'];
    
    report_file = io.open(filename, "r", encoding=encoding);
    
Last modified 3 years ago Last modified on Aug 25, 2016, 4:05:34 PM