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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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);
To translate the names of image files in a folder into English using Qt, you can follow these steps: 1. **Include necessary libraries**: First, make sure you have included the `QtWidgets` and `QtGui` libraries, as they provide file handling and translation support. ```cpp #include <QApplication> #include <QDir> #include <QTranslator> #include <QTextCodec> ``` 2. **Create a translator object**: Instantiate a `QTranslator` instance and load an English translation resource: ```cpp QTranslator translator; QFile langFile(QStringLiteral("en_US.qm")); // Replace "en_US" with your desired language code if (langFile.open(QIODevice::ReadOnly | QIODevice::Text)) { translator.load(langFile); } ``` 3. **Detect current locale**: Use `QLocale` to determine the user's system language if needed, or force it to English: ```cpp QLocale locale; if (!translator.loadFromResource(QStringLiteral("qt"), QStringLiteral("en"))) { // Forcing English locale = QLocale(QLatin1String("en")); } ``` 4. **Iterate over files and translate their names**: Loop through the images in the folder, get the original name, apply the translation, and update the file name: ```cpp QDir directory("path/to/folder"); foreach (const QString &fileName, directory.entryList()) { if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) { QString untranslatedName = QDir::toNativeSeparators(fileName); QString translatedName = translator.translate(untranslatedName); if (!translatedName.isEmpty()) { QString newFileName = translatedName + QLatin1String("."); // Add period back for file extension QFile source(untranslatedName); QFile target(newFileName); if (source.exists() && !target.exists()) { source.rename(target); } } } } ``` 5. **Handle exceptions**: Don't forget to handle any errors that might occur during file operations. 6. **Run the application**: Initialize your `QApplication` and run the main loop. ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); // ... Rest of your program logic return app.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值