lua cjson无法解码特定的Unicode字符?

在尝试解码特定的Unicode字符时,我收到了来自lua cjson的以下错误,

root@9dc8433e6d83:~/torch-rnn# th train.lua -input_h5 data/aud.h5 -input_json data/aud.json -batch_size 50 -seq_length 100 -rnn_size 256 -max_epochs 50
Running with CUDA on GPU 0
/root/torch/install/bin/luajit: train.lua:77: Expected value but found invalid unicode escape code at character 350873
stack traceback:
    [C]: in function 'read_json'
    train.lua:77: in main chunk
    [C]: in function 'dofile'
    /root/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk
    [C]: at 0x00406670

通过查看源代码,我可以看到train.lua read_json在内部使用cjson。

所涉及的Unicode escape代码是\uda85

如果我到https://www.branah.com/unicode-converter上,它会告诉我该转义应该解码为哪个字符。

该Unicode转义是使用Python unichr(55941)生成的,并通过Python脚本输出的重定向使用PYTHONIOENCODING = UTF-8写入文件。

以下演示了如何生成字符;

echo "print unichr(55941)" > test.py
python test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    print unichr(55941)
UnicodeEncodeError: 'ascii' codec can't encode character u'\uda85' in position 0: ordinal not in range(128)

# export PYTHONIOENCODING=UTF-8
# python test.py
���
# python test.py > tfile
# cat tfile
���
# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open("tfile",'r')
>>> s=f.readline()
>>> s
'\xed\xaa\x85\n'
>>> print s
���

>>> s.decode('utf-8')
u'\uda85\n'

总体而言,我尝试将0-65535范围内的一组整数使用Python映射到UTF-8字符并写入文件。然后,我想使用使用LUA的torch-rnn对字符序列进行RNN训练。当尝试运行th train.lua在torch-rnn python scripts / preprocess.py生成的文件上时,我遇到了错误。

点赞
用户909515
用户909515

似乎问题出在 Unicode 代理项字符上,理解这意味着我可以为不同的值进行过滤/转换。在这种用例中,这并不是什么大问题。

2016-09-04 19:17:01