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

Python中sys.stdout方法的語(yǔ)法示例詳解

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

Python中sys 模塊中的一個(gè)方法是stdout ,它使用其參數(shù)直接顯示在控制臺(tái)窗口上。

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

sys.stdout 方法的語(yǔ)法

sys.stdout

參數(shù)

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

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

示例:在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() 方法中傳遞的參數(shù)并在屏幕上顯示。

示例: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() 方法直接在控制臺(tái)顯示內(nèi)容,print() 語(yǔ)句有一個(gè)薄薄的stdout() 方法的包裝,也是對(duì)輸入的格式化。所以,默認(rèn)情況下,它在參數(shù)之間留有空格,并輸入一個(gè)新行。

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

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() 方法沒有給所提供的參數(shù)提供空間或新行。

示例:在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)

輸出結(jié)果:

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

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

示例:在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() 中,我們用它來(lái)評(píng)估一個(gè)在交互式 Python 會(huì)話中插入的表達(dá)式。

有一個(gè)異常處理程序有兩個(gè)選項(xiàng):如果參數(shù)值是可編碼的,那么就用backslashreplace 錯(cuò)誤處理程序進(jìn)行編碼。否則,如果它不是可編碼的,應(yīng)該用sys.std.errors 錯(cuò)誤處理程序進(jìn)行編碼。

示例:重復(fù)的sys.stdout 到一個(gè)日志文件

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

為了將輸出的控制臺(tái)結(jié)果存儲(chǔ)在一個(gè)文件中,我們可以使用open() 方法來(lái)存儲(chǔ)它。我們將所有的控制臺(tái)輸出存儲(chǔ)在同一個(gè)日志文件中。

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

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

相關(guān)文章

  • Celery批量異步調(diào)用任務(wù)一直等待結(jié)果問題

    Celery批量異步調(diào)用任務(wù)一直等待結(jié)果問題

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

    Python中sort和sorted函數(shù)代碼解析

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

    Django使用Celery異步任務(wù)隊(duì)列的使用

    這篇文章主要介紹了Django使用Celery異步任務(wù)隊(duì)列的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-03-03
  • python中取整數(shù)的幾種方法

    python中取整數(shù)的幾種方法

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

    Python使用socket模塊實(shí)現(xiàn)簡(jiǎn)單tcp通信

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

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

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

    Python與C/C++的相互調(diào)用案例

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

    將圖片文件嵌入到wxpython代碼中的實(shí)現(xiàn)方法

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

    Python數(shù)據(jù)分析與處理(一)--北京高考分?jǐn)?shù)線統(tǒng)計(jì)分析

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

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

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

最新評(píng)論