如何将Python中的双重UTF-8解码器代码翻译成Lua

3

我有这段遗留代码,它能够解码双重编码的UTF-8文本并还原为正常的UTF-8:

# Run with python3!
import codecs
import sys
s=codecs.open('doubleutf8.dat', 'r', 'utf-8').read()
sys.stdout.write(
                s
                .encode('raw_unicode_escape')
                .decode('utf-8')
        )

我需要将它翻译成Lua,并模仿所有可能的解码副作用(如果有的话)。
限制:我可以使用任何可用的Lua模块来处理UTF-8,但最好是稳定的,支持LuaRocks。我不会使用Lupa或其他Lua-Python桥接解决方案,也不会调用os.execute()来调用Python。
1个回答

3
你可以使用lua-iconv,它是与iconv库绑定的Lua程序。使用它,你可以随意转换字符编码。
它也可以在LuaRocks中获得。 编辑:使用这个答案,我已经能够正确地解码数据,使用以下Lua代码:
require 'iconv'
-- convert from utf8 to latin1
local decoder = iconv.new('latin1', 'utf8')
local data = io.open('doubleutf8.dat'):read('*a')
-- decodedData is encoded in utf8
local decodedData = decoder:iconv(data)
-- if your terminal understands utf8, prints "нижний новгород"
-- if not, you can further convert it from utf8 to any encoding, like KOI8-R
print(decodedData)

谢谢,但问题的重点是我对Python的UTF转换有些困惑(例如raw_unicode_escape是什么?),我想看到一段实际的Lua代码。抱歉在这里有些懒。 - Alexander Gladysh
一个样本文件会有所帮助,我不知道要期望什么数据,我会尝试使用lua-iconv制作一个示例。此外,“raw_unicode_escape”意味着:“生成适用于Python源代码中的原始Unicode文字面值的字符串”。 - Michal Kottman
虚假的数据(作为编码的Lua字符串字面量,连接字符串):" \ 034 \ 195 \ 144 \ 194 \ 189 \ 195 \ 144 \ 194 \ 184 \ 195 "。 "\ 144 \ 194 \ 189 \ 195 \ 144 \ 194 \ 184 \ 195 \ 144 \ 194 \ 185 \ 032 \ 195 \ 144" .. "\ 194 \ 189 \ 195 \ 144 \ 194 \ 190 \ 195 \ 144 \ 194 \ 178 \ 195 \ 144 \ 194 \ 179" .. "\ 195 \ 144 \ 194 \ 190 \ 195 \ 145 \ 194 \ 128 \ 195 \ 144 \ 194 \ 190 \ 195 \ 144" .. "\ 194 \ 180 \ 034" - Alexander Gladysh
希望我没有搞砸编码 :) - Alexander Gladysh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接