<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 從鍵盤讀入(標準輸入) 從鍵盤讀入后默認存為**字符串**,此處fd為字符串 ```python >>> fd = sys.stdin.read() hiyang ``` ### 標準輸出 ```python >>> sys.stdout.write(fd+'\n') hiyang 或者 >>> print fd ``` ### 錯誤輸出 ```python sys.stderr.write('hiyang') hiyang ``` ```python #!/usr/bin/env python import sys sys.stdout.write('this is stdout\n') sys.stderr.write('this is stderr\n') ``` 測試標準輸出和錯誤輸出 ```python # python std.py this is stdout this is stderr # python std.py 2> /dev/null this is stdout # python std.py > /dev/null this is stderr ``` --- ### 寫文件 **write**寫入方式,寫入的內容必須為字符串 >>> fd = open('/tmp/a.txt','w') >>> fd.write(str(123)) **print**格式寫入,追加還是覆蓋取決于前面的打開方式 >>> fd = open('/tmp/a.txt','w') >>> print >> fd, 'hiyang' #### 兩者關系 print >> sys.stdout,'hiyang' <==> sys.stdout.write('hiyang'+'\n') buffer 寫入緩存 ```python >>> for i in range(10): sys.stdout.write('%s' % i) sys.stdout.flush() #刷新緩存 time.sleep(1) ``` 執行腳本文件,默認以行為輸出單位 > 加'\n',或者加-u,或者 sys.stdout.flush()則無輸出緩存 ```python #!/usr/bin/env python import sys import time for i in range(10): sys.stdout.write('%s\n' % i) time.sleep(1) ``` #### '\-' 數據流 使用數據流進行遠程復制文件 ```python cat a.rpm | ssh ip 'cat - > a.rpm' ``` --- ### python 2 和 python 3 print語句的比較 | python 2.6 |python 3.0 | 說明 | |----|----|----| |print x, y | print(x, y) | 對象輸出sys.stdout.write(),并在各項間添加空格,末尾換行| |print x, y, | print(x, y , end='') | 同上,但是行末不換行 | |print >> afile, x, y |print(x, y, file=afile) | 把文件發送到myfile.write() | #### python 2 print語句向后兼容方法 ~~~ from __future__ import print_function from __future__ import print_function print('bad!' * 8, file=sys.stderr) bad!bad!bad!bad!bad!bad!bad!bad! ~~~ #### python 3 print函數 ``` print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout]) ``` `sep` 是在每個對象的文本之間插入一個字符串,默認是單個空格 `end` 添加在打印文本末尾的字符串,默認是'\n',傳遞一個空格將避免行尾換行 `file` 指定要發送到的文件,默認是sys.stdout;如果是真正的文件,文件應處于打開狀態 `print(x, y, z, sep='...', file=open('data.txt', w))` 要想更加具體靈活的格式,可以打印前提前格式化即可。 ### 自動化流重定向 通過賦值sys.stdout將打印文字重定向,需要先保存sys.stdout對象,完成后再恢復即可 在ipython下測試失敗,在python中測試OK ``` import sys tmp = sys.stdout sys.stdout = open('log.txt', 'a') print 'hiyang', print 'hiyang', sys.stdout.close() sys.stdout = tmp ``` ## Python input和raw_input的區別 使用input和raw_input都可以讀取控制臺的輸入,但是input和raw_input在處理數字時是有區別的 純數字輸入 當輸入為純數字時 input返回的是數值類型,如int,float raw_inpout返回的是字符串類型,string類型 輸入字符串為表達式 input會計算在字符串中的數字表達式,而raw_input不會。 如輸入 “57 + 3”: input會得到整數60 raw_input會得到字符串”57 + 3” python input的實現 看python input的文檔,可以看到input其實是通過raw_input來實現的,原理很簡單,就下面一行代碼: ~~~ def input(prompt): return (eval(raw_input(prompt))) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看