0

I am simply trying to read a text file that has 4000+ lines of nouns all single column and I’m getting an error:

Traceback (most recent call last):
  File "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/nouns.py", line 4, in <module>
    for i in nouns_file:
  File "/var/containers/Bundle/Application/107074CD-03B1-4FB3-809A-CBD44D6CF245/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/encodings/ascii.py", line 27, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2241: ordinal not in range(128)

With code:

with open("nounlist.txt", "r") as nouns_file:
    for i in nouns_file:
        print(i)

I’m not sure what’s causing this. I would think that it would just output all of the nouns from my nounlist.txt file.

  • Your file most likely has special characters that cannot be decoded, such as ñ. Does [this](https://stackoverflow.com/questions/24475393/unicodedecodeerror-ascii-codec-cant-decode-byte-0xc3-in-position-23-ordinal) answer your question? – Jake Tae Feb 08 '20 at 04:03
  • Text in computing may be more complicated and sensitive than you realize. Things don't "just work" unless by pure luck, or someone else has done the hard part for you, or usually both. There are several good resources about [encoding](http://kunststube.net/encoding/) you can read to increase your understanding – Hymns For Disco Feb 08 '20 at 04:07
  • 1
    Open file plus b most. Try running "rb" mode instead "r" mode for opening file. – dohuuhung Feb 08 '20 at 04:08
  • rb works and out puts b before the word and \n after so I believe I can just strip those and I’ll be good. Thanks! – Travis Barton Feb 08 '20 at 04:11
  • 1
    @TravisBarton the 'b' at the front denotes that the data is type `bytes`. You can't just strip it off. If `my_bytes = b'hello world!\n'` then you want to use `my_string = my_bytes.decode().rstrip('\n')` – Z4-tier Feb 08 '20 at 04:27
  • What's your question? Please [edit] to clarify. If you need help with debugging, you need to provide a [mre]. BTW welcome to Stack Overflow! Check out the [tour] and [ask]. – wjandrea Feb 08 '20 at 04:56
  • Thank you all for your help. I got it working :) – Travis Barton Feb 09 '20 at 05:31

0 Answers0