Jaffer Wilson
Jaffer Wilson

Reputation: 7273

Not able to read and edit file using Python 3

Here is what I have tried:

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

When I tried using the binary mode then it got loaded but I am not able to understand how to read and edit my own data in it.

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

Here is the file: symbols.raw

Please let me know how I can read it in human interpreted way and write my own data in it. Here is the format of the symbols.raw file.

Upvotes: 0

Views: 175

Answers (3)

albusSimba
albusSimba

Reputation: 469

One way is to read it off as bytes first then convert it into a list because python do not allow you to edit binary strings.

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

Alternatively you can slice the bytes according to your symbols files that you have provided, (assuming size is the number of bytes)

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

To write a new file you can just do the following:

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)

Upvotes: 0

kederrac
kederrac

Reputation: 17322

you may use with encoding="ISO-8859-1":

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

Upvotes: 2

incarnadine
incarnadine

Reputation: 700

You should be able to tell python to ignore or replace the errors by specifying the errors="ignore" parmater of the open function, as shown in this answer.

Upvotes: 0

Related Questions