Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read sys.stdin containing binary data in python (ignore errors)?

How do I read sys.stdin, but ignoring decoding errors? I know that sys.stdin.buffer exists, and I can read the binary data and then decode it with .decode('utf8', errors='ignore'), but I want to read sys.stdin line by line. Maybe I can somehow reopen the sys.stdin file but with errors='ignore' option?

like image 706
g00dds Avatar asked Aug 19 '26 18:08

g00dds


1 Answers

Found three solutions from here as Mark Setchell mentioned.

import sys
import io

def first():
    with open(sys.stdin.fileno(), 'r', errors='ignore') as f:
        return f.read()

def second():
    sys.stdin = io.TextIOWrapper(sys.stdin.buffer, errors='ignore')
    return sys.stdin.read()

def third():
    sys.stdin.reconfigure(errors='ignore')
    return sys.stdin.read()


print(first())
#print(second())
#print(third())

Usage:

$ echo 'a\x80b' | python solution.py
ab
like image 114
g00dds Avatar answered Aug 22 '26 07:08

g00dds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!