返回txt文档编码格式

  1. 安装chardet
pip install chardet
  1. 获得文本文档编码类型
import chardet
from chardet.universaldetector import UniversalDetector
def GetEncoding(file_path):
    with open(file_path, 'rb') as txt:
        detector = UniversalDetector()
        for line in txt.readlines():
            detector.feed(line)
            if detector.done:
                break
        detector.close()
    return detector.result

my_path = '.\\my_test.txt'
with open(my_path, 'rb') as f:
    str1 = f.read()
char_encoding= chardet.detect(str1)
print(f'字符串为:{str1}')
print(f'字符串编码信息为:{char_encoding}' ) 
print(f'字符串编码为: {char_encoding["encoding"]}')
print(f'附:{GetEncoding(my_path)}')

3. 把txt变成csv

import csv
import chardet

my_path = '.\\AuditTrail0.txt'
with open(my_path, 'rb') as f:
    str1 = f.read()
char_encoding= chardet.detect(str1)
encoding_format = char_encoding["encoding"]

csv_file = '.\\AuditTrail0.csv'

with open(my_path,'r',encoding=encoding_format) as file:
    lines = file.readlines()

with open(csv_file,'w',newline='',encoding=encoding_format) as file:
    writer = csv.writer(file)
    for line in lines:
        row = line.strip().split('\t')
        writer.writerow(row)
posted @   不愿透露姓名的小村村  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示