Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Python standard IO under Windows PowerShell and CMD

$
0
0

I have the following two-line Python (v. 3.10.7) program "stdin.py":

    import sys    print(sys.stdin.read())

and the following one-line text file "ansi.txt" (CP1252 encoding) containing:

‘I am well’ he said. 

Note that the open and close quotes are 0x91 and 0x92, respectively. In Windows-10 cmd mode the behavior of the Python code is as expected:

    python stdin.py < ansi.txt  # --> ‘I am well’ he said.

On the other hand in Windows Powershell:

    cat .\ansi.txt | python .\stdin.py  # --> ?I am well? he said.

Apparently the CP1252 characters are seen as non-printable characters in thecombination Python/PowerShell. If I replace in "stdin.py" the standard input by file input, Python prints correctly the CP1252 quote characters to screen. PowerShell by itself recognizes and prints correctly 0x91 and 0x92.

Questions: can somebody explain to me why cmd works differently than PowerShell in combination with Python? Why doesn't Python recognize the CP1252 quote characters 0x91 and 0x92 when they are piped into it by PowerShell?


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>