PacktPublishing / Deep-Learning-with-Keras

Code repository for Deep Learning with Keras published by Packt
MIT License
1.06k stars 716 forks source link

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

Closed mukesh-mehta closed 5 years ago

mukesh-mehta commented 6 years ago

ipython-input-11-cde6afe50c78 in module() 5 ftrain=open(os.path.join(Data_dir,"training.txt"),'rb') 6 for line in ftrain: ----> 7 label, sentence = line.strip().split("\t") 8 words=nltk.word_tokenize(sentence.decode().lower()) 9 if len(words)>maxlen:

I am using Python3.6

nielspetersen commented 6 years ago

Hi mukesh-metha,

did you try to open the training file via

ftrain = open(os.path.join(Data_dir,"training.txt"),'r')

I've made the experience that the examples in the book are written for Python 2.x. The binary option is not necessary in Python 3 since there have been some changes in the API (see https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit).

junxu-ai commented 5 years ago

lines = [] fin = open("../data/alice_in_wonderland.txt", "r") for line in fin: line = line.strip().encode("utf-8").decode("ascii", "ignore") if len(line) == 0: continue lines.append(line) fin.close()