【问题标题】:Not able to read and edit file using Python 3无法使用 Python 3 读取和编辑文件
【发布时间】:2020-07-13 01:11:57
【问题描述】:

这是我尝试过的:

>>> with open("symbols.raw") as f:
...     text=f.readlines()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Python35\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 1694: character maps to <undefined>
>>> with open("symbols.raw",encoding='utf-16') as f:
...     text=f.readlines()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Python35\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
  File "C:\Python35\lib\encodings\utf_16.py", line 61, in _buffer_decode
    codecs.utf_16_ex_decode(input, errors, 0, final)
UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 7500-7501: illegal encoding
>>> with open("symbols.raw",encoding='utf-8') as f:
...     text=f.readlines()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Python35\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 7: invalid start byte

当我尝试使用二进制模式时,它被加载了,但我无法理解如何在其中读取和编辑我自己的数据。

>>> with open("symbols.raw",'rb') as f:
...     text=f.readlines()
...

这是文件:symbols.raw

请告诉我如何以人类解释的方式阅读它并在其中写入我自己的数据。 这是format of the symbols.raw file

【问题讨论】:

  • 为什么不将文件读取为二进制文件,然后将其解码为 utf-16?
  • @Avenger789 会不会和读取第二个错误中显示的 utf-16 编码的文件一样出现问题?
  • 如您所见,我已经尝试过了。但不工作。其次,我说过我无法理解二进制文件。我已经写在问题中了。
  • 你可以使用'rb'来阅读它,然后使用struct mod 来解释它,你可以在docs.python.org/3/library/struct.html#module-struct阅读文档
  • @Avenger789 尝试了你的想法:错误:UnicodeError: UTF-16 stream does not start with BOM

标签: python python-3.x file


【解决方案1】:

你可以使用encoding="ISO-8859-1":

with open("symbols.raw", encoding="ISO-8859-1") as f:
    text=f.readlines()

【讨论】:

  • 哇,我可以编辑了。但请你也帮我编辑文件。因为我无法理解二进制文件。 Itried,因此我发现了一些与格式相关的东西。你能帮我解决这个问题吗?
  • 我在问题中提到我正在尝试读取和编辑文件。已经在同一个问题中提到过。请问你能帮我吗?
【解决方案2】:

您应该能够通过指定打开函数的errors="ignore" 参数来告诉python 忽略或替换错误,如this 答案所示。

【讨论】:

    【解决方案3】:

    一种方法是先将其作为字节读取,然后将其转换为列表,因为 python 不允许您编辑二进制字符串。

    def read_file_bytes(file_name):
        in_file = open(file_name, "rb")  
        data = in_file.read() 
        in_file.close()    
        return data
    
    file_data = list(read_file_bytes(file_name))
    

    或者,您可以根据您提供的符号文件对字节进行切片,(假设大小是字节数)

    file_data = read_file_bytes(file_name)
    name = str(file_data[:12])
    unknown_2 = int(file_data[1628:1628 + 4])
    

    要编写新文件,您只需执行以下操作:

    def write_bytes_to_file(file_name, bytes):
        out_file = open(file_name, "wb")
        out_file.write(bytes)
        out_file.close()
    
    all_bytes = bytearray(name) + bytearray(unknown_2)
    write_bytes_to_file('new_file_name.raw', all_bytes)
    

    【讨论】:

    • 但是如何将我的数据编辑到其中?请问你也可以添加那部分吗?我也有疑问,我正在尝试读取和编辑文件
    • 你可以根据已经编辑的值。或者你的意思是写一个新的原始文件
    • 我猜写新文件总是安全的。我正在尝试将自己的值添加到文件中。这就是为什么我在处理二进制数据时非常困惑。
    • 公平地说,这个问题可能有 2 个部分,并且您需要的所有东西都已经提供。剩下的就是根据您提供的顺序读取文件。
    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2018-10-24
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2016-04-06
    相关资源
    最近更新 更多