简体   繁体   中英

How to read .pkl file to a tokenizer object?

I use

 pickle.dump(tokenizer, open(TOKENIZER_MODEL, "wb"), protocol=0)

to generate a.pkl file, when I trying to read it using

pickle.load(open('tokenizer.pkl'))

it raises

TypeError: a bytes-like object is required, not 'str'

but when I trying to change this object to bytes-like object using

tkr = open('tokenizer.pkl')
bytes(tkr,encoding='utf-8')

it raises

TypeError: encoding without a string argument

I was wodering that if there is anyway to read this.pkl file or io.TextIOWrapper file to a tokenizer object?

When you want to read the file you need to write

file = open('tokenizer.pkl', 'rb')    
pickle.load(file)
  • 'r' stands for read mode
  • 'b' stands for binary mode

Ok I've never used pickle but after trying it really quickly in repl.it I think that your problem is in this line

pickle.load(open('tokenizer.pkl'))

It should be

pickle.load(open('tokenizer.pkl','rb'))

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