Home > Blockchain >  python stdin: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0:
python stdin: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0:

Time:03-11

I'm trying to use stdin(standard input) as input for my codes.

this is how I have implement for now. I was trying to see whether it give output that I am intended.

import sys

for i in sys.stdin.readlines():
    print(i.strip())

what I intend was printing multiple lines of string where it gives

(venv) Test@Test-MacBookPro pythonProject1 % /Users/Test/PycharmProjects/pythonProject1/venv/bin/python /Users/Test/PycharmProjects/pythonProject1/proto_1.py < /Users/Test/PycharmProjects/pythonProject1/venv/bin/python /Users/Test/PycharmProjects/pythonProject1/input.txt    
Traceback (most recent call last):
  File "/Users/Test/PycharmProjects/pythonProject1/proto_1.py", line 3, in <module>
    for i in sys.stdin.readlines():
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte

this is input file that I used:

apple
banana
carrot
ddddd
eeeee
fffff

does anyone have idea why this happens?

CodePudding user response:

Yes, I do. Let's look at your command line:

(venv) Test@Test-MacBookPro pythonProject1 % /Users/Test/PycharmProjects/pythonProject1/venv/bin/python /Users/Test/PycharmProjects/pythonProject1/proto_1.py < /Users/Test/PycharmProjects/pythonProject1/venv/bin/python /Users/Test/PycharmProjects/pythonProject1/input.txt    

Removing the paths just to make it more clear:

python proto_1.py < python input.txt

You are passing the Python interpreter executable as your input file. Why did you do that? Just pass the file name:

/Users/Test/PycharmProjects/pythonProject1/venv/bin/python /Users/Test/PycharmProjects/pythonProject1/proto_1.py < /Users/Test/PycharmProjects/pythonProject1/input.txt 
  • Related