Hướng dẫn change stdin python - thay đổi stdin python

Tôi đang sử dụng máy Windows và Linux cho cùng một dự án. Mã hóa mặc định cho STDIN trên Windows là CP1252 và trên Linux là UTF-8.

Tôi muốn thay đổi mọi thứ thành UTF-8. Có thể không? Tôi làm nó như thế nào?

Câu hỏi này là về Python 2; Đối với Python 3, xem Python 3: Cách chỉ định mã hóa stdin

Hướng dẫn change stdin python - thay đổi stdin python

hỏi ngày 29 tháng 4 năm 2010 lúc 14:07Apr 29, 2010 at 14:07

Bạn có thể làm điều này bằng cách không dựa vào mã hóa ngầm khi in mọi thứ. Không dựa vào đó là một ý tưởng tốt trong mọi trường hợp - mã hóa ngầm chỉ được sử dụng khi in ra stdout và khi stdout được kết nối với thiết bị đầu cuối.

Một cách tiếp cận tốt hơn là sử dụng

UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'
8 ở khắp mọi nơi và sử dụng
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'
9 hoặc
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
0 ở khắp mọi nơi. Bạn bọc
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
1 trong một đối tượng tự động mã hóa các chuỗi Unicode của bạn thành UTF-8, ví dụ:

sys.stdout = codecs.getwriter('utf-8')(sys.stdout)

Điều này sẽ chỉ hoạt động nếu bạn sử dụng Unicode ở mọi nơi, mặc dù. Vì vậy, sử dụng Unicode ở mọi nơi. Thực sự, ở khắp mọi nơi.

Đã trả lời ngày 29 tháng 4 năm 2010 lúc 14:12Apr 29, 2010 at 14:12

Thomas Woutersthomas WoutersThomas Wouters

127K23 Huy hiệu vàng146 Huy hiệu bạc122 Huy hiệu Đồng23 gold badges146 silver badges122 bronze badges

4

Đây là một câu hỏi cũ, nhưng chỉ để tham khảo.

Để đọc

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
2 từ
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3, sử dụng:

UTF8Reader = codecs.getreader('utf8')
sys.stdin = UTF8Reader(sys.stdin)

# Then, e.g.:
for _ in sys.stdin:
    print _.strip()

Để viết

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
2 lên
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5, sử dụng:

UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'

Đã trả lời ngày 11 tháng 12 năm 2014 lúc 14:58Dec 11, 2014 at 14:58

Hướng dẫn change stdin python - thay đổi stdin python

1

Python tự động phát hiện mã hóa Stdin. Cách đơn giản nhất tôi đã tìm thấy để chỉ định mã hóa khi phát hiện tự động không hoạt động đúng là sử dụng biến môi trường Pythonioencoding, như trong ví dụ sau:

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py

Để biết thêm thông tin về phát hiện mã hóa và biến này trên các nền tảng khác nhau, bạn có thể xem tài liệu sys.stdin.

Đã trả lời ngày 6 tháng 2 năm 2016 lúc 2:35Feb 6, 2016 at 2:35

Johnfjohnfjohnf

2632 Huy hiệu bạc8 Huy hiệu Đồng2 silver badges8 bronze badges

Một đoạn mã đơn giản mà tôi đã sử dụng, phù hợp với tôi trên Ubuntu: Python2.7 và Python3.6

from sys import version_info
if version_info.major == 2:  # for python2
    import codecs
    # for stdin
    UTF8Reader = codecs.getreader('utf8')
    sys.stdin = UTF8Reader(sys.stdin)
    # for stdout
    UTF8Writer = codecs.getwriter('utf8')
    sys.stdout = UTF8Writer(sys.stdout)
elif version_info.major == 3:  # for python3
    import codecs
    # for stdin
    UTF8Reader = codecs.getreader('utf8')
    sys.stdin = UTF8Reader(sys.stdin.buffer)
    # for stdout
    UTF8Writer = codecs.getwriter('utf8')
    sys.stdout = UTF8Writer(sys.stdout.buffer)

Đã trả lời ngày 18 tháng 10 năm 2019 lúc 11:30Oct 18, 2019 at 11:30

Trước khi trải qua bài viết này, hãy để chúng tôi hiểu những điều khoản

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3,
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5 và
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
8 là gì.

Nội dung chính ShowShow

  • 1. sys.stdin
  • 2. sys.stdout
  • Đoạn trích trên tiếp tục đọc đầu vào từ stdin và in thông báo vào bảng điều khiển (stdout) cho đến khi gặp từ Hi Message from sys.stdin: ---> Hi Hello from AskPython
  • Lưu ý: Chúng tôi thường không đóng đối tượng tệp stdin mặc định, mặc dù nó được cho phép. Vì vậy, Hi Message from sys.stdin: ---> Hi Hello from AskPython
  • Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython
  • Điều này tương tự như Hi Message from sys.stdin: ---> Hi Hello from AskPython
  • Làm thế nào để bạn đọc Stdin và Stdout trong Python?
  • Làm thế nào để bạn sử dụng stdin trong python?
  • Stdout hoạt động như thế nào trong Python?
  • Làm thế nào để bạn viết thư cho stdout trong Python?

Đầu vào tiêu chuẩn-Đây là xử lý tệp mà chương trình người dùng đọc để lấy thông tin từ người dùng. Chúng tôi cung cấp đầu vào cho đầu vào tiêu chuẩn (stdin). – This is the file-handle that a user program reads to get information from the user. We give input to the standard input (stdin). – This is the file-handle that a user program reads to get information from the user. We give input to the standard input (stdin).

Đầu ra tiêu chuẩn-Chương trình người dùng ghi thông tin bình thường vào xử lý tệp này. Đầu ra được trả về thông qua đầu ra tiêu chuẩn (stdout). – The user program writes normal information to this file-handle. The output is returned via the Standard output (stdout). – The user program writes normal information to this file-handle. The output is returned via the Standard output (stdout).

Lỗi tiêu chuẩn-Chương trình người dùng ghi thông tin lỗi cho xử lý tệp này. Lỗi được trả về thông qua lỗi tiêu chuẩn (STDERR). – The user program writes error information to this file-handle. Errors are returned via the Standard error (stderr). – The user program writes error information to this file-handle. Errors are returned via the Standard error (stderr).

Python cung cấp cho chúng tôi các đối tượng giống như tệp đại diện cho stdin, stdout và stderr. Chúng ta hãy xem xét cách chúng ta có thể làm việc với các đối tượng này để làm việc với đầu vào và đầu ra của chương trình.file-like objects that represent stdin, stdout, and stderr. Let us look at how we could work with these objects to work with the input and output of our program.file-like objects that represent stdin, stdout, and stderr. Let us look at how we could work with these objects to work with the input and output of our program.


1. sys.stdin

2. sys.stdout

Đoạn trích trên tiếp tục đọc đầu vào từ stdin và in thông báo vào bảng điều khiển (stdout) cho đến khi gặp từ Hi Message from sys.stdin: ---> Hi Hello from AskPython

Lưu ý: Chúng tôi thường không đóng đối tượng tệp stdin mặc định, mặc dù nó được cho phép. Vì vậy, Hi Message from sys.stdin: ---> Hi Hello from AskPython

import sys

stdin_fileno = sys.stdin

# Keeps reading from stdin and quits only if the word 'exit' is there
# This loop, by default does not terminate, since stdin is open
for line in stdin_fileno:
    # Remove trailing newline characters using strip()
    if 'exit' == line.strip():
        print('Found exit. Terminating the program')
        exit(0)
    else:
        print('Message from sys.stdin: ---> {} <---'.format(line))

Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program

Điều này tương tự như Hi Message from sys.stdin: ---> Hi Hello from AskPython

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
4.

Làm thế nào để bạn đọc Stdin và Stdout trong Python?

Làm thế nào để bạn sử dụng stdin trong python?6 là mã Python hợp lệ.: We do not normally close the default
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3 file object, although it is allowed. So

Stdout hoạt động như thế nào trong Python?

Làm thế nào để bạn viết thư cho stdout trong Python?


2. sys.stdout

Đoạn trích trên tiếp tục đọc đầu vào từ stdin và in thông báo vào bảng điều khiển (stdout) cho đến khi gặp từ Hi Message from sys.stdin: ---> Hi Hello from AskPython

Lưu ý: Chúng tôi thường không đóng đối tượng tệp stdin mặc định, mặc dù nó được cho phép. Vì vậy, Hi Message from sys.stdin: ---> Hi Hello from AskPython

Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
9.
UTF8Reader = codecs.getreader('utf8')
sys.stdin = UTF8Reader(sys.stdin)

# Then, e.g.:
for _ in sys.stdin:
    print _.strip()
4

Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython

UTF8Reader = codecs.getreader('utf8')
sys.stdin = UTF8Reader(sys.stdin)

# Then, e.g.:
for _ in sys.stdin:
    print _.strip()
5

Đoạn trích trên tiếp tục đọc đầu vào từ stdin và in thông báo vào bảng điều khiển (stdout) cho đến khi gặp từ Hi Message from sys.stdin: ---> Hi Hello from AskPython

Làm thế nào để bạn đọc Stdin và Stdout trong Python?

Làm thế nào để bạn sử dụng stdin trong python?Exceptions and Error messages. (Which is why it is called Standard Error).

Làm thế nào để bạn viết thư cho stdout trong Python?

UTF8Reader = codecs.getreader('utf8')
sys.stdin = UTF8Reader(sys.stdin)

# Then, e.g.:
for _ in sys.stdin:
    print _.strip()
7

Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython

UTF8Reader = codecs.getreader('utf8')
sys.stdin = UTF8Reader(sys.stdin)

# Then, e.g.:
for _ in sys.stdin:
    print _.strip()
8

Điều này tương tự như Hi Message from sys.stdin: ---> Hi Hello from AskPython

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
4.

Lưu ý: Chúng tôi thường không đóng đối tượng tệp stdin mặc định, mặc dù nó được cho phép. Vì vậy, Hi Message from sys.stdin: ---> Hi Hello from AskPython

Bây giờ chúng ta đã biết một chút về

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3, chúng ta hãy chuyển sang
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5.

2. sys.stdout

Đối với đối tượng tệp đầu ra, chúng tôi sử dụng

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
9. Nó tương tự như
Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
1, nhưng nó hiển thị trực tiếp bất cứ thứ gì được ghi vào nó vào bảng điều khiển.
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'
2

Đầu ra

Đoạn trích trên tiếp tục đọc đầu vào từ

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3 và in thông báo vào bảng điều khiển (
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5) cho đến khi gặp từ

UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'
34.

Lưu ý: Chúng tôi thường không đóng đối tượng tệp

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3 mặc định, mặc dù nó được cho phép. Vì vậy,

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
6 là mã Python hợp lệ.

Bây giờ chúng ta đã biết một chút về

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3, chúng ta hãy chuyển sang
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5.

2. sys.stdout

Đối với đối tượng tệp đầu ra, chúng tôi sử dụng

Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
9. Nó tương tự như
Hi
Message from sys.stdin: ---> Hi
 <---
Hello from AskPython
Message from sys.stdin: ---> Hello from AskPython
 <---
exit
Found exit. Terminating the program
1, nhưng nó hiển thị trực tiếp bất cứ thứ gì được ghi vào nó vào bảng điều khiển.
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

# Then, e.g.:
print 'Anything'
2

Đầu ra

Đoạn trích trên tiếp tục đọc đầu vào từ

pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
3 và in thông báo vào bảng điều khiển (
pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py
5) cho đến khi gặp từ

Lưu ý: Chúng tôi thường không đóng đối tượng tệp pipeline | PYTHONIOENCODING="UTF-8" /path/to/your-script.py 3 mặc định, mặc dù nó được cho phép. Vì vậy,

  • Hi
    Message from sys.stdin: ---> Hi
     <---
    Hello from AskPython
    Message from sys.stdin: ---> Hello from AskPython
     <---
    exit
    Found exit. Terminating the program
    
    6 là mã Python hợp lệ.
  • Hi
    Message from sys.stdin: ---> Hi
     <---
    Hello from AskPython
    Message from sys.stdin: ---> Hello from AskPython
     <---
    exit
    Found exit. Terminating the program
    
    9. Nó tương tự như
    Hi
    Message from sys.stdin: ---> Hi
     <---
    Hello from AskPython
    Message from sys.stdin: ---> Hello from AskPython
     <---
    exit
    Found exit. Terminating the program
    
    1, nhưng nó hiển thị trực tiếp bất cứ thứ gì được ghi vào nó vào bảng điều khiển.

Đoạn trích dưới đây cho thấy chúng ta có đầu ra vào bảng điều khiển nếu chúng ta viết thư cho Hi Message from sys.stdin: ---> Hi Hello from AskPython used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input.