How to Correct AttributeError

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

How can I fix the error below?

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

See

for the appropriate lines.

around line 33

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

around line 249

243f=open(param[1], encoding='utf-8')
 244 text = f.read()
 245 f.close()
 246
 247# print(text)
 248
 249chain= PrepareChain(text)
 250 triplet_freqs=chain.make_triplet_freqs()
 251 chain.save (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.

text=text.encode('utf-8')

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.
https://karaage.hatenadiary.jp/entry/2016/01/27/073000

If so, I think this page will be helpful.
Using https://qiita.com/betit0919/items/4fbba42de6df90bc7088#text-generator

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.