返回txt文档编码格式

1. 安装chardet

1
pip install chardet

2. 获得文本文档编码类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import chardet
from chardet.universaldetector import UniversalDetector
def GetEncoding(file):
"""
:param file:
:return: {'encoding': '', 'confidence': 0.99, 'language': ''}
"""
txt = open(file, "rb")
detector = UniversalDetector()
for line in txt.readlines():
detector.feed(line)
if detector.done:
break
detector.close()
txt.close()
return detector.result

my_path = 'C:\\Users\\xiaocuncun\\Desktop\\my1200_web\\my_test.txt'
f = open(my_path, 'rb')
str1 = f.read()
char_encoding= chardet.detect(str1)
print(f'字符串为:{str1}')
print(f'字符串编码信息为:{char_encoding}' )
print(f'字符串编码为: {char_encoding["encoding"]}')

3. 把txt变成csv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)

返回txt文档编码格式
http://example.com/2024/07/21/返回txt文档编码格式/
作者
xiao cuncun
发布于
2024年7月21日
许可协议