How to Correct AttributeError

Asked 2 months ago, Updated 2 months ago, 2 views

How can I fix the error below?

 C:\UsersDesktop>python sample.txt
Traceback (most recent call last):
  File "", line 249, in<module>
  File "", line 33, in __init__
AttributeError: 'str' object has no attribute'decode'


for the appropriate lines.

around line 33

32 if isinstance(text,str):
33 text = text.decode ("utf-8")

around line 249

243f=open(param[1], encoding='utf-8')
 244 text =
 245 f.close()
 247# print(text)
 249chain= PrepareChain(text)
 250 triplet_freqs=chain.make_triplet_freqs()
 251 (triplet_freqs, True)

That's it.

python python3

2022-09-30 11:03

2 Answers

The decode method is the method used to convert byte strings.Therefore, the string object does not have a decode method.
This error points out that.Use the encode method to convert a string object to a byte string.


However, encoding is specified at the time of file opening, so this process may not be necessary.

2022-09-30 11:03

Perhaps you are trying to run a program written in python2 on python3 (because the decode of str is not in python3).

I would like to move the TextGenerator as shown below.

If so, I think this page will be helpful.

The link above automatically converts python2 code to python3 using 2to3 and then manually corrects the questions you have.

I think it would be easier to get answers if you write not only the problem but also the prerequisites.

2022-09-30 11:03

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.