Unable to edit the contents of the .txt file because it is gibberish

Viewed 556

I have obtained a data.txt file from an online source. When I open the file with Notepad, I see the random characters as shown in the figure.

Screen shot

I attempted to open the file using the following python code snippet:

my_file = 'data.txt'
f = open(my_file, 'rb')
print(f)
ff = pickle.load(f)
print(ff)
f.close()

The first print operation gives <_io.BufferedReader name='data.txt'>in the console. And the second print operation displays all the data of data.txt file in a readable form.

I want to edit the data in the data.txt file with my own data sets. I googled for possible solutions. Most of the available solutions (for example this) suggest changing the Encoding scheme of the data.txt file to UTF-8. At present, the data.txt Encoding is ANSI. I changed the Encoding to UTF-8 as suggested. However, the problem still persists (file still contains gibberish). Moreover, I tried to see the contents of the file (now UTF-8 encoding) using the above python code snippet. This time, I get the following error.

_pickle.UnpicklingError: invalid load key, '\xef'.

The python code shows that the file has valid data. However, I'm unable to edit the data with my own data sets. Any help, please!

1 Answers

The error:

_pickle.UnpicklingError: invalid load key, '\xef'.

means that the load key:\xef isn't plain text. This could be an image, music file, etc. If the contents of the .txt file is not plain text there is no way to convert the characters to text.

Related