{"rsdb":{"rid":"397503","subhead":"","postdate":"0","aid":"273596","fid":"77","uid":"1","topic":"1","content":"
\n

Python\u4e2dsys \u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u662fstdout \uff0c\u5b83\u4f7f\u7528\u5176\u53c2\u6570\u76f4\u63a5\u663e\u793a\u5728\u63a7\u5236\u53f0\u7a97\u53e3\u4e0a\u3002<\/p> \n

\u8fd9\u4e9b\u79cd\u7c7b\u7684\u8f93\u51fa\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\uff0c\u50cf\u4e00\u4e2a\u7b80\u5355\u7684\u6253\u5370\u8bed\u53e5\uff0c\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u6216\u8005\u4e00\u4e2a\u8f93\u5165\u63d0\u793a\u3002print() \u65b9\u6cd5\uff0c\u5b83\u6709\u76f8\u540c\u7684\u884c\u4e3a\uff0c\u9996\u5148\u8f6c\u6362\u4e3asys.stdout() \u65b9\u6cd5\uff0c\u7136\u540e\u5728\u63a7\u5236\u53f0\u663e\u793a\u7ed3\u679c\u3002<\/p> \n

sys.stdout \u65b9\u6cd5\u7684\u8bed\u6cd5<\/h3> \n
sys.stdout\n<\/code><\/pre> \n 

\u53c2\u6570<\/strong><\/p> \n

\u4e0d\u6d89\u53ca\u4efb\u4f55\u53c2\u6570\u3002\u6211\u4eec\u4f7f\u7528sys.stdout \u4f5c\u4e3a\u8f93\u51fa\u6587\u4ef6\u5bf9\u8c61\u3002
\u8fd4\u56de\u503c<\/strong><\/p> \n

\u8be5\u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u53ea\u5728\u63a7\u5236\u53f0\u76f4\u63a5\u663e\u793a\u8f93\u51fa\u3002<\/p> \n

\u793a\u4f8b\uff1a\u5728Python\u4e2d\u4f7f\u7528sys.stdout \u65b9\u6cd5<\/h3> \n
# import the sys module to use methods\nimport sys\nsys.stdout.write('This is my first line')\nsys.stdout.write('This is my second line')\n<\/code><\/pre> \n 

\u8f93\u51fa\uff1a<\/p> \n

This is my first line This is my second line\n<\/code><\/pre> \n 

\u5b83\u5c06\u8fd4\u56desys.stdout.write() \u65b9\u6cd5\u4e2d\u4f20\u9012\u7684\u53c2\u6570\u5e76\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u3002<\/p> \n

\u793a\u4f8b\uff1asys.stdout.write() \u4e0eprint() \u65b9\u6cd5<\/h3> \n
import sys\n# print shows new line at the end\nprint("First line ")\nprint("Second line ")\n# displays output directly on console without space or newline\nsys.stdout.write('This is my first line ')\nsys.stdout.write('This is my second line ')\n# for inserting new line\nsys.stdout.write("n")\nsys.stdout.write('In new line ')\n# writing string values to file.txt\nprint('Hello', 'World', 2+3, file=open('file.txt', 'w'))\n<\/code><\/pre> \n 

\u8f93\u51fa\uff1a<\/p> \n

First line\nSecond line\nThis is my first line This is my second line\nIn new line\n# file.txt will be created with text "Hello World 5" as a string\n<\/code><\/pre> \n 

\u6211\u4eec\u4f7f\u7528sys.stdout.write() \u65b9\u6cd5\u76f4\u63a5\u5728\u63a7\u5236\u53f0\u663e\u793a\u5185\u5bb9\uff0cprint() \u8bed\u53e5\u6709\u4e00\u4e2a\u8584\u8584\u7684stdout() \u65b9\u6cd5\u7684\u5305\u88c5\uff0c\u4e5f\u662f\u5bf9\u8f93\u5165\u7684\u683c\u5f0f\u5316\u3002\u6240\u4ee5\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u5728\u53c2\u6570\u4e4b\u95f4\u7559\u6709\u7a7a\u683c\uff0c\u5e76\u8f93\u5165\u4e00\u4e2a\u65b0\u884c\u3002<\/p> \n

\u5728Python 3.0\u7248\u672c\u4e4b\u540e\uff0cprint() \u65b9\u6cd5\u4e0d\u4ec5\u63a5\u53d7stdout() \u65b9\u6cd5\uff0c\u8fd8\u63a5\u53d7\u4e00\u4e2a\u6587\u4ef6\u53c2\u6570\u3002\u4e3a\u4e86\u7ed9\u51fa\u4e00\u4e2a\u884c\u7684\u7a7a\u683c\uff0c\u6211\u4eec\u628a"n" \u4f20\u7ed9stdout.write() \u65b9\u6cd5\u3002
\u793a\u4f8b\u4ee3\u7801\uff1a\u4f7f\u7528sys.stdout.write() \u65b9\u6cd5\u6765\u663e\u793a\u4e00\u4e2a\u5217\u8868<\/p> \n

import sys\n# sys.stdout assigned to "carry" variable\ncarry = sys.stdout\nmy_array = ['one', 'two', 'three']\n# printing list items here\nfor index in my_array:\n    carry.write(index)\n<\/code><\/pre> \n 

\u8f93\u51fa\uff1a<\/p> \n

# prints a list on a single line without spaces\nonetwothree\n<\/code><\/pre> \n 

\u8f93\u51fa\u663e\u793a\uff0cstdout.write() \u65b9\u6cd5\u6ca1\u6709\u7ed9\u6240\u63d0\u4f9b\u7684\u53c2\u6570\u63d0\u4f9b\u7a7a\u95f4\u6216\u65b0\u884c\u3002<\/p> \n

\u793a\u4f8b\uff1a\u5728Python\u4e2d\u4f7f\u7528sys.stdout.flush() \u65b9\u6cd5<\/h3> \n
import sys\n# import for the use of the sleep() method\nimport time\n# wait for 5 seconds and suddenly shows all output\nfor index in range(5):\n    print(index, end =' ')\n    time.sleep(1)\nprint()\n# print one number per second till 5 seconds\nfor index in range(5):\n    # end variable holds \/n by default\n    print(index, end =' ')\n    sys.stdout.flush()\n    time.sleep(1)\n<\/code><\/pre> \n 

\u8f93\u51fa\u7ed3\u679c\uff1a<\/p> \n

0 1 2 3 4 # no buffer\n0 1 2 3 4 # use buffer\n<\/code><\/pre> \n 

sys.stdout.flush() \u65b9\u6cd5\u5237\u65b0\u4e86\u7f13\u51b2\u533a\u3002\u8fd9\u610f\u5473\u7740\u5b83\u5c06\u628a\u7f13\u51b2\u533a\u7684\u4e1c\u897f\u5199\u5230\u63a7\u5236\u53f0\uff0c\u5373\u4f7f\u5b83\u5728\u5199\u4e4b\u524d\u4f1a\u7b49\u5f85\u3002<\/p> \n

\u793a\u4f8b\uff1a\u5728Python\u4e2d\u4f7f\u7528sys.stdout.encoding() \u65b9\u6cd5<\/h3> \n
# import sys module for stdout methods\nimport sys\n# if the received value is not None, then the function prints repr(receivedValue) to sys.stdout\ndef display(receivedValue):\n    if receivedValue is None:\n        return\n    mytext = repr(receivedValue)\n    # exception handling\n    try:\n        sys.stdout.write(mytext)\n    # handles two exceptions here\n    except UnicodeEncodeError:\n        bytes = mytext.encode(sys.stdout.encoding, 'backslashreplace')\n        if hasattr(sys.stdout, 'buffer'):\n            sys.stdout.buffer.write(bytes)\n        else:\n            mytext = bytes.decode(sys.stdout.encoding, 'strict')\n            sys.stdout.write(mytext)\n    sys.stdout.write("n")\ndisplay("my name")\n<\/code><\/pre> \n 

\u8f93\u51fa\uff1a<\/p> \n

'my name'\n<\/code><\/pre> \n 

\u65b9\u6cd5sys.stdout.encoding() \u7528\u4e8e\u6539\u53d8sys.stdout \u7684\u7f16\u7801\u3002\u5728\u65b9\u6cd5display() \u4e2d\uff0c\u6211\u4eec\u7528\u5b83\u6765\u8bc4\u4f30\u4e00\u4e2a\u5728\u4ea4\u4e92\u5f0f Python \u4f1a\u8bdd\u4e2d\u63d2\u5165\u7684\u8868\u8fbe\u5f0f\u3002<\/p> \n

\u6709\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u7a0b\u5e8f\u6709\u4e24\u4e2a\u9009\u9879\uff1a\u5982\u679c\u53c2\u6570\u503c\u662f\u53ef\u7f16\u7801\u7684\uff0c\u90a3\u4e48\u5c31\u7528backslashreplace \u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u8fdb\u884c\u7f16\u7801\u3002\u5426\u5219\uff0c\u5982\u679c\u5b83\u4e0d\u662f\u53ef\u7f16\u7801\u7684\uff0c\u5e94\u8be5\u7528sys.std.errors \u9519\u8bef\u5904\u7406\u7a0b\u5e8f\u8fdb\u884c\u7f16\u7801\u3002<\/p> \n

\u793a\u4f8b\uff1a\u91cd\u590d\u7684sys.stdout \u5230\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6<\/h3> \n
import sys\n# method for multiple log saving in txt file\nclass multipleSave(object):\n    def __getattr__(self, attr, *arguments):\n        return self._wrap(attr, *arguments)\n    def __init__(self, myfiles):\n        self._myfiles = myfiles\n    def _wrap(self, attr, *arguments):\n","orderid":"0","title":"Python\u6559\u7a0b\uff1asys.stdout\u65b9\u6cd5(\u4e00)","smalltitle":"","mid":"0","fname":"Python","special_id":"0","bak_id":"0","info":"0","hits":"601","pages":"2","comments":"0","posttime":"2023-09-23 15:44:25","list":"1695455065","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Python<\/A> \u6559\u7a0b<\/A> sys.stdout<\/A> \u65b9\u6cd5<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Python\u6559\u7a0b\uff1asys.stdout\u65b9\u6cd5","lastview":"1715646703","digg_num":"622","digg_time":"1715789204","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}