python读写UCS-2 big/little endian(utf-16-be/utf-16-le)格式的文件

来源:互联网 发布:深度linux deepin安装 编辑:程序博客网 时间:2024/05/18 03:17

def readFile(path):
    oFile = file(path, 'r')
    line = ""
    line = oFile.readline()
    encoding = None
    lst = []
    if line.startswith('/xFF/xFE'):
        encoding = 'utf-16-le'
        oFile.close()
        import codecs
        oFile = codecs.open(path, 'r', encoding)
        lst = oFile.readlines()
        oFile.stream.close()

原创粉丝点击