[英]Python - Convert Octal to non-English Text from file
I am trying to convert Non-English file encoded in Octal back into it's native format and store it in another file. 我试图将Octal编码的非英文文件转换回原始格式并将其存储在另一个文件中。 The files include:
文件包括:
Sample i_file 示例i_file
\340\262\270\340\263\215-\340\262\207+\340\262\241\340\263\215
\340\262\205-\340\262\246\340\263\215+\340\262\255\340\263\215
\340\262\205-\340\262\252\340\263\215+\340\262\252\340\263\215
\340\262\250\340\263\215-\340\262\205+\340\262\265\340\263\215
\340\262\205+\340\262\246\340\263\215
\340\262\266\340\263\215-\340\262\206+\340\262\270\340\263\215
\340\262\246\340\263\215-\340\262\205+\340\262\252\340\263\215
\340\262\244\340\263\215-\340\262\205+\340\262\237\340\263\215 \340\262\250\340\263\215-\340\262\205+\340\262\265\340\263\215
\340\262\247\340\263\215-\340\262\205
\340\262\247\340\263\215-\340\262\212
\340\262\205-\340\262\234\340\263\215+\340\262\206
\340\262\263\340\263\215-\340\262\205
\340\262\263\340\263\215-\340\262\207
\340\262\263\340\263\215-\340\262\211
\340\262\212+\340\262\263\340\263\215 \340\262\247\340\263\215-\340\262\212
sp
\340\262\256\340\263\215-\340\262\217+\340\262\262\340\263\215
\340\262\254\340\263\215-\340\262\216+\340\262\237\340\263\215
\340\262\260\340\263\215-\340\262\205+\340\262\271\340\263\215
\340\262\252\340\263\215+\340\262\260\340\263\215 \340\262\205-\340\262\252\340\263\215+\340\262\252\340\263\215
\340\262\265\340\263\215-\340\262\207+\340\262\270\340\263\215 \340\262\270\340\263\215-\340\262\207+\340\262\241\340\263\215
\340\262\217-\340\262\225\340\263\215+\340\262\205
\340\262\211+\340\262\227\340\263\215 \340\262\263\340\263\215-\340\262\211
\340\262\243\340\263\215-\340\262\205+\340\262\227\340\263\215
\340\262\212-\340\262\256\340\263\215+\340\262\254\340\263\215
\340\262\250\340\263\215-\340\262\216+\340\262\263\340\263\215
\340\262\216+\340\262\244\340\263\215
\340\262\205-\340\262\260\340\263\215+\340\262\256\340\263\215
\340\262\260\340\263\215+\340\262\205
\340\262\260\340\263\215+\340\262\206 \340\262\260\340\263\215+\340\262\205
\340\262\260\340\263\215+\340\262\207
\340\262\260\340\263\215+\340\262\212 \340\262\260\340\263\215+\340\262\207
\340\262\260\340\263\215+\340\262\223 \340\262\260\340\263\215+\340\262\207
\340\262\255\340\263\215-\340\262\205+\340\262\246\340\263\215
\340\262\205-\340\262\247\340\263\215+\340\262\257\340\263\215
\340\262\211-\340\262\237\340\263\215+\340\262\211
\340\262\206+\340\262\225\340\263\215
\340\262\205-\340\262\260\340\263\215 \340\262\205-\340\262\260\340\263\215+\340\262\256\340\263\215
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\206 \340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\207 \340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\211 \340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\212 \340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\222 \340\262\250\340\263\215-\340\262\250\340\263\215+\340\262\205
\340\262\220-\340\262\250\340\263\215
\340\262\206-\340\262\252\340\263\215+\340\262\206 \340\262\205-\340\262\252\340\263\215+\340\262\252\340\263\215
\340\262\206-\340\262\252\340\263\215+\340\262\223 \340\262\205-\340\262\252\340\263\215+\340\262\252\340\263\215
\340\262\222-\340\262\246\340\263\215+\340\262\205
\340\262\225\340\263\215-\340\262\222+\340\262\237\340\263\215
\340\262\205-\340\262\270\340\263\215+\340\262\205
\340\262\205-\340\262\270\340\263\215+\340\262\207
\340\262\205-\340\262\256\340\263\215+\340\262\270\340\263\215 \340\262\212-\340\262\256\340\263\215+\340\262\254\340\263\215
\340\262\244\340\263\215-\340\262\205+\340\262\260\340\263\215
\340\262\230\340\263\215-\340\262\205 \340\262\263\340\263\215-\340\262\205
\340\262\265\340\263\215-\340\262\206+\340\262\227\340\263\215 \340\262\266\340\263\215-\340\262\206+\340\262\270\340\263\215
\340\262\270\340\263\215-\340\262\205+\340\262\270\340\263\215 \340\262\244\340\263\215-\340\262\205+\340\262\260\340\263\215
\340\262\244\340\263\215-\340\262\211+\340\262\265\340\263\215
\340\262\257\340\263\215
The code i thought would work would be to convert the text into a byte array using bytearray()
, then decode it to utf-8
and write it to the targe file. 我认为可行的代码是使用
bytearray()
将文本转换为字节数组,然后将其解码为utf-8
并将其写入目标文件。 octal_to_text.py octal_to_text.py
"""
Convert file contents from Octal to text
"""
with open('i_file','r') as tl, open('o_file','w+') as tk:
for line in tl.readlines():
line = (line.strip())
br = bytearray(line)
tk.write("{}\n".format(br.decode('utf-8')))
In the code above however, the output file generated is the same as the input. 但是,在上面的代码中,生成的输出文件与输入相同。
bytearray
doesn't seem to be doing anything. bytearray
似乎没有做任何事情。 What exactly am I doing wrong here? 我到底错在了什么? Could you provide a python2.7 solution?
你能提供python2.7解决方案吗?
NOTE The output file should contain characters like the ones shown below 注意输出文件应包含如下所示的字符
ಅ
ಆ
ಇ
ಈ
ಉ
ಊ
ಋ
ಎ
ಏ
ಐ
ಒ
ಓ
ಔ
ಕ್
ಖ್
ಗ್
ಘ್
ಚ್
ಛ್
ಜ್
ಝ್
ಟ್
ಠ್
ಡ್
ಢ್
ಣ್
The print
statement for an Octal encoded string, decodes Octal to the non-English notation only when working the python interpreter. 八进制编码字符串的
print
语句仅在使用python解释器时将八进制解码为非英语符号。
Thus, a simple work around is to perform the following steps: 因此,一个简单的解决方法是执行以下步骤:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.