V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
soratadori
V2EX  ›  问与答

如何读取 Unicode 的文本?

  •  
  •   soratadori · Mar 24, 2015 · 2313 views
    This topic created in 4064 days ago, the information mentioned may be changed or developed.

    用Windows的记事本创建一个编码为unicode的文本,但怎么也读不出来。如果保存成ANSI和UTF-8的话都可以读的出来。

    虽然用想办法用一些其他的办法读出来了,但还是不太懂原理,为什么普通的办法读不出来?
    

    错误代码:
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0: illegal multibyte sequence

    python的代码:
    file = open('unicode.txt','r+')
    file = file.read() #这里试过decode/encode成utf-8 gbk 都不行

    print(file)

    1 replies    2015-03-24 01:49:17 +08:00
    yfdyh000
        1
    yfdyh000  
       Mar 24, 2015
    试了一下,那个选项保存的是UTF-16LE BOM。
    http://stackoverflow.com/questions/22459020/python-decode-utf-16-file-with-bom
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5047 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 03:57 · PVG 11:57 · LAX 20:57 · JFK 23:57
    ♥ Do have faith in what you're doing.