How to detect the encoding of a text file with Deno?

本文介绍如何使用Python和Deno检测文本文件的编码格式。通过chardet库,Python能够检测多种常见编码如UTF-8、ASCII等。Deno则通过char-encoding-detector模块实现类似功能。


How to detect the encoding of a text file with Python?

Python 中有一个有用的包 - chardet,它有助于检测文件中使用的编码。实际上,没有程序可以 100% 确信使用了哪种编码 - 这就是为什么 chardet 以最高概率对文件进行编码的原因。Chardet 可以检测以下编码:

ASCII、UTF-8、UTF-16(2 种变体)、UTF-32(4 种变体)
Big5, GB2312, EUC-TW, HZ-GB-2312, ISO-2022-CN (繁体中文和简体中文)
EUC-JP, SHIFT_JIS, CP932, ISO-2022-JP (日语)
EUC-KR, ISO-2022-KR(韩语)
KOI8-R, 麦西里尔文, IBM855, IBM866, ISO-8859-5, windows-1251 (西里尔文)
ISO-8859-2, 窗口-1250 (匈牙利语)
ISO-8859-5, 窗口-1251 (保加利亚语)
窗口-1252 (英语)
ISO-8859-7, 视窗-1253 (希腊文)
ISO-8859-8, windows-1255 (视觉和逻辑希伯来语)
TIS-620 (泰语)
您可以使用 pip 命令安装 chardet:

pip install chardet

之后,您可以在命令行中使用 chardet:

% chardetect somefile someotherfile
somefile: windows-1252 with confidence 0.5
someotherfile: ascii with confidence 1.0

或者在 Python 中:

import chardet    
rawdata = open(file, "r").read()
result = chardet.detect(rawdata)
charenc = result['encoding']

How to detect the encoding of a text file with Deno?

import { detectEncoding } from "https://dev.jspm.io/char-encoding-detector@0.0.9";
const file = "ex2022.txt";
const text = await Deno.readFile(file);
const encoding = detectEncoding(text);
const decoder = new TextDecoder(encoding);
const correct = decoder.decode(text);
console.log(correct);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值