Python-从文件将八进制转换为非英语文本 [英] Python - Convert Octal to non-English Text from file

查看:89
本文介绍了Python-从文件将八进制转换为非英语文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以 Octal 编码的非英语文件转换回其本机格式,并将其存储在另一个文件中。这些文件包括:

I am trying to convert Non-English file encoded in Octal back into it's native format and store it in another file. The files include:


  • i_file:输入的八进制编码文本原始文件

  • o_file:输出目标文件,其中应包含卡纳达语(有问题的非英语)文本。

  • octal_to_text.py:应该在输入文件中包含八进制文本并在目标文件中生成对应的非英​​语文本的python程序。

示例 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

我认为可行的代码是使用 bytearray()将文本转换为字节数组,然后将其解码为 utf-8 并将其写入目标文件。 octal_to_text.py

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. 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')))

但是在上面的代码中,生成的输出文件是与输入相同。 bytearray 似乎没有任何作用。我到底在做什么错?您能提供python2.7解决方案吗?

In the code above however, the output file generated is the same as the input. bytearray doesn't seem to be doing anything. What exactly am I doing wrong here? Could you provide a python2.7 solution?

注意输出文件应包含如下所示的字符

NOTE The output file should contain characters like the ones shown below

ಅ
ಆ
ಇ
ಈ
ಉ
ಊ
ಋ
ಎ
ಏ
ಐ
ಒ
ಓ
ಔ
ಕ್
ಖ್
ಗ್
ಘ್
ಚ್
ಛ್
ಜ್
ಝ್
ಟ್
ಠ್
ಡ್
ಢ್
ಣ್


推荐答案

八进制编码字符串的 print 语句将八进制解码为非八进制-仅在使用python解释器时才使用英语符号。

The print statement for an Octal encoded string, decodes Octal to the non-English notation only when working the python interpreter.

因此,一个简单的解决方法是执行以下步骤:

Thus, a simple work around is to perform the following steps:


  1. 复制要解码的文件的内容

  2. 打开python解释器,然后将文件内容分配给多行字符串中的
    变量。

  3. 打印变量

  4. 将内容复制到新文件

  1. Copy the contents of the file to be decoded
  2. Open the python interpreter and assign the file contents to a variable in a multi line string.
  3. Print the variable
  4. Copy contents to a new file

这篇关于Python-从文件将八进制转换为非英语文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆