• print 参数


    import  time,sys
    def process(percent,width=50):
        if percent >= 1:
            percent=1
        show_str=('[%%-%ds]'%width)%(int(width*percent)*'#')
        print('
    %s %d %%'%(show_str,int(100*percent)),file=sys.stdout,flush=True,end='')
    # :每行的输出开始位置都在行首
    #end='',不换行输出
    #flush: 立刻输出,不缓存
    #file:输出位置
    #%% :输出%
    #输出结果,字符串,数字,% data_size
    =1000 recv_size=0 while recv_size < data_size: time.sleep(0.2) recv_size+=100 percent=recv_size/data_size process(percent,width=70)

    [######################################################################] 100 %

    参考print的官方文档

    print(...)
    print(value, ..., sep=' ', end='
    ', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    
      • 在python中,print默认向屏幕输出指定的文字,例如:

        >>>print('hello,world')
        hello world

      • print的完整格式为print(objects,sep,end,file,flush),其中后面4个为可选参数

        1. sep
          在输出字符串之间插入指定字符串,默认是空格,例如:
          >>>print("a","b","c",sep="**")
          a**b**c
        2. end
          print输出语句的结尾加上指定字符串,默认是换行( ),例如:
          >>>print("a",end="$")
          a$
          print默认是换行,即输出语句后自动切换到下一行,对于python3来说,如果要实现输出不换行的功能,那么可以设置end=''(python2可以在print语句之后加“,”实现不换行的功能)
        3. file
          将文本输入到file-like对象中,可以是文件,数据流等等,默认是sys.stdout
          >>>f = open('abc.txt','w')
          >>>print('a',file=f)
        4. flush
          flush值为True或者False,默认为Flase,表示是否立刻将输出语句输入到参数file指向的对象中(默认是sys.stdout)例如:
          >>>f = open('abc.txt','w')
          >>>print('a',file=f)
          可以看到abc.txt文件这时为空,只有执行f.close()之后才将内容写进文件。
          如果改为:
          >>>print('a',file=f,flush=True)
          则立刻就可以看到文件的内容
  • 相关阅读:
    Mac OS使用brew安装memcached
    Mac OS使用brew安装memcached
    Mac OS使用brew安装memcached
    JAVA学习之路 (五) 类
    JAVA学习之路 (五) 类
    JAVA学习之路 (五) 类
    JAVA学习之路 (五) 类
    常用的CSS小技巧
    常用的CSS小技巧
    常用的CSS小技巧
  • 原文地址:https://www.cnblogs.com/wuxi9864/p/9907591.html
Copyright © 2020-2023  润新知