简体   繁体   English

Python - 从文件中将八进制转换为非英语文本

[英]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: 文件包括:

  • i_file : The input Original File with Octal Encoded text i_file:带有八进制编码文本的输入原始文件
  • o_file : The output Target File which should contain the Kannada(The non-English Language in question) text. o_file:输出目标文件,其中应包含Kannada(所讨论的非英语语言)文本。
  • octal_to_text.py : The python program that should take the octal text in in the input file and generate corrusponding non-English text in the target file. octal_to_text.py:python程序,它应该在输入文件中输入八进制文本并在目标文件中生成相应的非英语文本。

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: 因此,一个简单的解决方法是执行以下步骤:

  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. 打开python解释器并将文件内容分配给多行字符串中的变量。
  3. Print the variable 打印变量
  4. Copy contents to a new file 将内容复制到新文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM