0

I'm trying to look at the readability scores for a text file using this Python code:

posts = open("/Users/jchavis/Desktop/Data/posts.txt","r")

readability_scores = Textatistic(posts).scores

flesch = readability_scores['flesch_score']

print('The Flesch Reading Ease is %.2f' % (flesch))

However, I receive this error:

AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

I'm not sure what needs to be corrected. Any help would be greatly appreciated!

JC91
  • 11
  • 3
  • Presumably, `Textastics` expects a string, not a file-like object from which a string can be read. Maybe `Textastics(posts.read()).scores`? (I'm basing this on the fact that `replace` is a `str` method.) – chepner Jul 13 '22 at 18:21
  • @chepner Thanks for your reply. I get the following error when I try that approach: "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte" – JC91 Jul 13 '22 at 18:23
  • Then the file you are opening isn't UTF-8-encoded; provide the correct encoding with `open`'s `encoding` argument. – chepner Jul 13 '22 at 18:40

0 Answers0