![](/img/trans.png)
[英]How to convert encoding of text file (which contains text of language other than English) from “UTF-16 LE” to “UTF-8” in Python?
[英]File encoding from English text to UTF-8
如何在Python中將帶有CRLF行終止符的非ISO擴展ASCII英文文本轉換為utf-8
擴展Jishiyu的答案,您可以使用uchardet來標識字符集。 例如
iconv -f `uchardet a_strange_file.txt` -t UTF-8 -o the_output_file.txt a_strange_file.txt
雖然這在python中不起作用。
我認為linux命令unix2dos,dos2unix,iconv會有所幫助。
像
iconv -f latin-1 -t UTF-8 latin.txt> utf8.txt
如果獲得輸入文件的原始字節流,則可以將其decode
為utf-8
。 請參閱此博客文章以及一些Python 3示例。
我已經使用enca庫創建了一個自動轉換腳本 ,我在NAS上使用了該腳本將字幕轉換為UTF-8,但是它可以用於任何自動轉換
隨時使用:)
編輯:
#!/bin/bash
LANGUAGE=czech
TO=utf8
CONVERT="enca -L $LANGUAGE -x $TO"
# Find and onvert
find ./ -type f -name "*.srt" | while read fn; do
IS_TARGET=`enca "${fn}" | egrep -ow -m 1 'UTF-8|Unrecognized|KOI8-CS2|7bit ASCII|UCS-2|Macintosh Central European'`
if [ "$IS_TARGET" != "UTF-8" ] &&
[ "$IS_TARGET" != "UCS-2" ] &&
[ "$IS_TARGET" != "Macintosh Central European" ] &&
[ "$IS_TARGET" != "Unrecognized" ] &&
[ "$IS_TARGET" != "7bit ASCII" ] &&
[ "$IS_TARGET" != "KOI8-CS2" ]; then
echo "${fn} ---- Will be converted!"
# optional backup of original srt
# cp "${fn}" "${fn}.bak"
$CONVERT "${fn}"
fi
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.