0

I am writing a code to extract specific lines from a text file into an output file, and here is what my code looks like:

with open('output.txt', 'w') as outfile:
        with open('testfile.txt') as infile:
            for line in infile.readlines:
                if 'Emotion' in line or 'PANS.RESP' in line:
                    outfile.write(line)
        outfile.write('-----------------------------------------------------')

I keep getting this error:

Traceback (most recent call last): File "/Users/Sam/Desktop/readstuff.py", line 3, in for line in infile: File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 154: ordinal not in range(128)

What should I do?

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) –  Nov 10 '17 at 19:39
  • shouldnt `infile.readlines` be `infile.readlines()` - I highly doubt line contains any text .... its a "functionpointer" or smth like it (not a python crack here) – Patrick Artner Nov 10 '17 at 19:43
  • Simply put, you have to read a text file with the character encoding it was written with. Somehow, you should know which encoding that is before you write your code. Then, you write your code to read with that encoding. – Tom Blodget Nov 11 '17 at 20:10

0 Answers0