解码Ebcdic [英] Decoding Ebcdic
问题描述
我正在传递ebcdic编码的数据。
I'm being passed data that is ebcdic encoded. Something like:
s = u'@@@@@@@@@@@@@@@@@@@ÂÖÉâÅ@ÉÄ'
尝试 .decode('cp500')
是错误的,但是正确的方法是什么?如果我将字符串复制到类似Notepad ++的程序中,则可以将其从EBCDIC转换为ascii,但似乎无法在python中找到可行的方法来实现这一点。对于它的价值,正确的结果是: BOISE ID
(正负空格填充)。
Attempting to .decode('cp500')
is wrong, but what's the correct approach? If I copy the string into something like Notepad++ I can convert it from EBCDIC to ascii, but I can't seem to find a viable approach in python to achieve the same. For what it's worth, the correct result is: BOISE ID
(plus or minus space padding).
信息从JSON对象行的文件中检索。该文件如下所示:
The information is being retrieved from a file of lines of JSON objects. That file looks like this:
{ "command": "flush-text", "text": "@@@@@O@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O" }
{ "command": "flush-text", "text": "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\u00C9\u00C4@\u00D5\u00A4\u0094\u0082\u0085\u0099z@@@@@@@@@@\u00D9\u00F5\u00F9\u00F7\u00F6\u00F8\u00F7\u00F2\u00F4" }
{ "command": "flush-text", "text": "@@@@@OmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmO" }
{ "command": "flush-text", "text": "@@@@@O@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O" }
处理循环如下所示:
with open('myfile.txt', 'rb') as fh:
for line in fh:
data = json.loads(line)
推荐答案
如果Notepad ++将其转换为OK,那么您只需要:
If Notepad++ converts it ok, then you should simply need:
Python 2.7:
with io.open('myfile.txt', 'r', encoding="cp500") as fh:
for line in fh:
data = json.loads(line)
Python 3.x:
with open('myfile.txt', 'r', encoding="cp500") as fh:
for line in fh:
data = json.loads(line)
当使用给定的解码读取文件时,它使用TextWrapper对其进行解码。 io
模块为Python 2.x提供Python 3 open
,并提供编解码器/ TextWrapper和通用换行支持
This uses a TextWrapper to decode the file as it's read using the given decoding. io
module provides Python 3 open
to Python 2.x, with codecs/TextWrapper and universal newline support
这篇关于解码Ebcdic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!