Python读取Unicode编码格式的文件(转换为UTF-8)

时间:2023-01-05 13:16:00

我不说话,只写代码。

import re


def filter_null(c):
    if not re.match('[\\x00\\xff\\xfe]', c):
        return True
    
if __name__ == '__main__':
    save = open('save.txt', 'w') # save.txt是转换之后的utf-8文件
    try:
        with open('sec.txt', 'rb') as f: # sec.txt是以Unicode编码的文件,不限于txt格式
            for line in open('sec.txt'):
                line = f.readline()
                line = filter(filter_null, line)
                line = line[:-1]  # 去掉多余的换行符
                line = line.encode('utf-8')
                print line
                save.writelines(line)
    finally:
        save.close()
                
    with open('save.txt', 'r') as f_utf8:
        print f_utf8.read()
        pass