欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中sys.stdout方法的語法示例詳解

 更新時間:2023年09月13日 14:57:20   作者:小小程序員ol  
Python中sys 模塊中的一個方法是stdout ,它使用其參數直接顯示在控制臺窗口上,print() 方法,它有相同的行為,首先轉換為sys.stdout() 方法,然后在控制臺顯示結果,本文給大家介紹Python sys.stdout方法的語法,感興趣的朋友一起看看吧

Python中sys 模塊中的一個方法是stdout ,它使用其參數直接顯示在控制臺窗口上。

這些種類的輸出可以是不同的,像一個簡單的打印語句,一個表達式,或者一個輸入提示。print() 方法,它有相同的行為,首先轉換為sys.stdout() 方法,然后在控制臺顯示結果。

sys.stdout 方法的語法

sys.stdout

參數

不涉及任何參數。我們使用sys.stdout 作為輸出文件對象。
返回值

該方法不返回任何值,只在控制臺直接顯示輸出。

示例:在Python中使用sys.stdout 方法

# import the sys module to use methods
import sys
sys.stdout.write('This is my first line')
sys.stdout.write('This is my second line')

輸出:

This is my first line This is my second line

它將返回sys.stdout.write() 方法中傳遞的參數并在屏幕上顯示。

示例:sys.stdout.write() 與print() 方法

import sys
# print shows new line at the end
print("First line ")
print("Second line ")
# displays output directly on console without space or newline
sys.stdout.write('This is my first line ')
sys.stdout.write('This is my second line ')
# for inserting new line
sys.stdout.write("n")
sys.stdout.write('In new line ')
# writing string values to file.txt
print('Hello', 'World', 2+3, file=open('file.txt', 'w'))

輸出:

First line
Second line
This is my first line This is my second line
In new line
# file.txt will be created with text "Hello World 5" as a string

我們使用sys.stdout.write() 方法直接在控制臺顯示內容,print() 語句有一個薄薄的stdout() 方法的包裝,也是對輸入的格式化。所以,默認情況下,它在參數之間留有空格,并輸入一個新行。

在Python 3.0版本之后,print() 方法不僅接受stdout() 方法,還接受一個文件參數。為了給出一個行的空格,我們把"n" 傳給stdout.write() 方法。
示例代碼:使用sys.stdout.write() 方法來顯示一個列表

import sys
# sys.stdout assigned to "carry" variable
carry = sys.stdout
my_array = ['one', 'two', 'three']
# printing list items here
for index in my_array:
    carry.write(index)

輸出:

# prints a list on a single line without spaces
onetwothree

輸出顯示,stdout.write() 方法沒有給所提供的參數提供空間或新行。

示例:在Python中使用sys.stdout.flush() 方法

import sys
# import for the use of the sleep() method
import time
# wait for 5 seconds and suddenly shows all output
for index in range(5):
    print(index, end =' ')
    time.sleep(1)
print()
# print one number per second till 5 seconds
for index in range(5):
    # end variable holds /n by default
    print(index, end =' ')
    sys.stdout.flush()
    time.sleep(1)

輸出結果:

0 1 2 3 4 # no buffer
0 1 2 3 4 # use buffer

sys.stdout.flush() 方法刷新了緩沖區(qū)。這意味著它將把緩沖區(qū)的東西寫到控制臺,即使它在寫之前會等待。

示例:在Python中使用sys.stdout.encoding() 方法

# import sys module for stdout methods
import sys
# if the received value is not None, then the function prints repr(receivedValue) to sys.stdout
def display(receivedValue):
    if receivedValue is None:
        return
    mytext = repr(receivedValue)
    # exception handling
    try:
        sys.stdout.write(mytext)
    # handles two exceptions here
    except UnicodeEncodeError:
        bytes = mytext.encode(sys.stdout.encoding, 'backslashreplace')
        if hasattr(sys.stdout, 'buffer'):
            sys.stdout.buffer.write(bytes)
        else:
            mytext = bytes.decode(sys.stdout.encoding, 'strict')
            sys.stdout.write(mytext)
    sys.stdout.write("n")
display("my name")

輸出:

'my name'

方法sys.stdout.encoding() 用于改變sys.stdout 的編碼。在方法display() 中,我們用它來評估一個在交互式 Python 會話中插入的表達式。

有一個異常處理程序有兩個選項:如果參數值是可編碼的,那么就用backslashreplace 錯誤處理程序進行編碼。否則,如果它不是可編碼的,應該用sys.std.errors 錯誤處理程序進行編碼。

示例:重復的sys.stdout 到一個日志文件

import sys
# method for multiple log saving in txt file
class multipleSave(object):
    def __getattr__(self, attr, *arguments):
        return self._wrap(attr, *arguments)
    def __init__(self, myfiles):
        self._myfiles = myfiles
    def _wrap(self, attr, *arguments):
        def g(*a, **kw):
            for f in self._myfiles:
                res = getattr(f, attr, *arguments)(*a, **kw)
            return res
        return g
sys.stdout = multipleSave([ sys.stdout, open('file.txt', 'w') ])
# all print statement works here
print ('123')
print (sys.stdout, 'this is second line')
sys.stdout.write('this is third linen')

輸出:

# file.txt will be created on the same directory with multiple logs in it.
123
<__main__.multipleSave object at 0x00000226811A0048> this is second line
this is third line

為了將輸出的控制臺結果存儲在一個文件中,我們可以使用open() 方法來存儲它。我們將所有的控制臺輸出存儲在同一個日志文件中。

這樣,我們可以存儲任何打印到控制臺的輸出,并將其保存到日志文件中。

到此這篇關于Python中sys.stdout方法的文章就介紹到這了,更多相關Python sys.stdout方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Celery批量異步調用任務一直等待結果問題

    Celery批量異步調用任務一直等待結果問題

    這篇文章主要介紹了Celery批量異步調用任務一直等待結果問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Python中sort和sorted函數代碼解析

    Python中sort和sorted函數代碼解析

    這篇文章主要介紹了Python中sort和sorted函數代碼解析,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Django使用Celery異步任務隊列的使用

    Django使用Celery異步任務隊列的使用

    這篇文章主要介紹了Django使用Celery異步任務隊列的使用,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • python中取整數的幾種方法

    python中取整數的幾種方法

    這篇文章主要給大家分享python中取整數的幾種方法技巧,文章將圍繞python取整數的詳細的相關資料展開內容,需要的朋友可以參考一下,希望對你有所幫助
    2021-11-11
  • Python使用socket模塊實現簡單tcp通信

    Python使用socket模塊實現簡單tcp通信

    這篇文章主要介紹了Python使用socket模塊實現簡單tcp通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • python操作XML格式文件的一些常見方法

    python操作XML格式文件的一些常見方法

    最近有同學詢問如何利用Python處理xml文件,特此整理一篇比較簡潔的操作手冊,下面這篇文章主要給大家介紹了關于python操作XML格式文件的一些常見方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Python與C/C++的相互調用案例

    Python與C/C++的相互調用案例

    這篇文章主要介紹了Python與C/C++的相互調用案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 將圖片文件嵌入到wxpython代碼中的實現方法

    將圖片文件嵌入到wxpython代碼中的實現方法

    前面一篇文章中提到的那個程序,GUI中包含了一張圖片。在編譯成exe文件發(fā)布時,無法直接生成一個單獨的exe文件。因此需要直接把圖片寫入到代碼中
    2014-08-08
  • Python數據分析與處理(一)--北京高考分數線統計分析

    Python數據分析與處理(一)--北京高考分數線統計分析

    這篇文章主要介紹了Python數據分析與處理北京高考分數線統計分析,文章問繞Python數據分析與處理相關資料的介紹,展開對北京高考分數線統計分析,需要的小伙伴可以參考一下
    2021-12-12
  • 解決pandas.DataFrame.fillna 填充Nan失敗的問題

    解決pandas.DataFrame.fillna 填充Nan失敗的問題

    今天小編就為大家分享一篇解決pandas.DataFrame.fillna 填充Nan失敗的問題。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11

最新評論