简体   繁体   中英

How to convert dat file to readable text using python

I am techy102 and I want to know how to convert a dat file to readable text using python, for an upcoming project I wanted to know this information, here is my code:

import pickle
x = input("enter something: ")
with open('savefile.dat', 'wb') as f:
    pickle.dump(x, f, protocol=2)
with open('savefile.dat','rb') as ff:
    print(ff)

and it gives me this:

<_io.BufferedReader name='savefile.dat'>

Does anyone know how to actually convert the contents to readable text?

You have to unpickle what you pickle using the pickle.load function.

import pickle
x = input("enter something: ")
with open('savefile.dat', 'wb') as f:
    pickle.dump(x, f, protocol=2)

with open('savefile.dat','rb') as ff:
    x1 = pickle.load(ff)

print(x1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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