input and output abbreviated python

python Input / Output
1) Output:
  1, expression statements
  2, print () function
  3, write the file object () (the standard output file can be referenced using sys.stdout)
2) output format:
  1, str.format ( ): formatted output value
    a, and inside the parentheses characters (referred to as field formatted) will be the format parameter () replaces:
      EG: >>> Print ( '{} first Sen:! {}'. format ( 'yangrongkuan', 'a student at South China Normal'))

      # Output: yangrongkuan first Sen: studying at the China division!
    b, figures in brackets for the location of the object point to the incoming format () in:
      EG: >>> Print (. '{0} and {1}' format ( 'yang ', 'rong')) # output: yang and Rong
        >>> Print (. '. 1} and {{0}' format ( 'rong ', 'kuan')) # output: kuan and Rong
    C, If the keyword parameter format () in , they will point to a value of the parameter name:
      EG: >>> Print ( '{name} brother: {role}'. format ( name = ' Young', role = 'is a student '))

      # Output: YangDaGe: IS A Student
    D, and the position can be any combination of the key parameters:
      EG: >>> Print ( 'Student sequence {0}, {1}, and {a}.' Format ( 'yang'. , 'rong', a = ' kuan'))

      # Output: a list of student Yang, rong, and Kuan
    E, optional ':' and format identifier may follow the field name. After Pi retained to three decimal places:: The
      EG: Import Math >>>
        >>> Print ( 'PI is approximately constant values {0: .3f}.' Format (math.pi).) # Output: 3.142
    F, in the ':' after passed an integer, can ensure that the field has a width of at least so much. Such as:
      EG: Student >>> = { 'yangrongkuan':}. 1
        >>> for name, table.items Number in ():
        ... Print ( '{0:20} ==> {. 1: 1Od}' .format (name, number)) # == guaranteed output> before and after the width
    g, the processing format string long, but do not want to separate them, the variable name can be accomplished by: incoming dictionary, then use '[]' to access the key.
      EG: Table >>> = { 'Yang':. 1, 'Rong': 2, 'Kuan':}. 3
        >>> Print ( 'Kuan: {0 [Kuan]: D}; Yang: {0 [Yang] : d}; rong:. { 0 [rong]: d} 'format (table))

        # Output: Kuan:. 3; Yang:. 1; Rong: 2

  2, the repr () and str () can be converted into a value of the output string.
    Difference: repr () may be produced in the form of an expression parser readable; STR () Returns a user readable expressions.
  3, str.rjust (): the string may be right, left and filled space. ; (Center, so that the string centered around ljust fill space, its left, up space on the right)
  . 4, str.zfill (width): length width specified string (including symbols such as a decimal point, a negative sign, etc. ). Original string right justified, to be filled with 0 until the specified width size in the left figure, if the original string has a length greater than or equal to the value of width, not any operation.
    eg: >>> '12'.zfill (5) # Output:' 00012 '
      >>>' -3.14'.zfill (. 7) # Output: -003.14
       >>> '3.14159265359'.zfill (. 5) # Output: 3.14159265359
  5, the old string formatting: '%': the left argument of a formatted string, and substituting the right, and then returns the formatted string:
    EG: Import Math >>>
      >>> Print ( ' PI is approximately constant value:% 5.3f '% math.pi) # output: the value of PI is approximately constant: 3. 142.

3) reads keyboard input: input (): from standard input reads a line of text, the default standard input is a keyboard. Further a python expression can also receive input, and outputs the result as.
  eg: >>> str = input ( "Please enter:");
    >>> Print ( "what you type is:", str)
  Output:
    Please enter: yangrongkuan
    content of your output is: yangrongkuan

4) to read and write files
  1, grammar: open (filename, mode): open () returns a file object: filename string value that represents the name of the file you want to visit; mode represents the open mode of the file.
  Annex: mode parameter is not mandatory. The file open mode: ( '+': append; 'B': Open the file in binary format)
    R & lt: Read-only file. Pointer file will be placed at the beginning of the file. This is the default mode.
    rb: open a file in binary format for read-only. The file pointer will be placed at the beginning of the file.
    r +: open a file for reading and writing. The file pointer will be placed at the beginning of the file. ((Rb +: opened in binary format))

    w: only opens a file for writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted (all content). If the file does not exist, create a new file. (Wb: Open in binary format)

    w +: open a file for reading and writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted (all content). If the file does not exist, create a new file. (Wb: Open in binary format)

     a: Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing. (Ab: Open in binary format)

    a+:打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写。(ab+:以二进制格式打开)

  实例:
    eg:
      # 打开一个文件
      f = open("D:/test.txt", "w")#直接复制文件所在路径会出现'\','/'的问题,window系统文件的路径是'\'
      f.write( "Python 是一个非常好的语言。\n是的,的确非常好!!\n" )
      # 关闭打开的文件
      f.close()
  备注:直接复制文件所在路径会出现'\','/'的问题,window系统文件的路径是'\',python用的也是'\'作为转义字符,所以会导致解析器解析文件路径错误。可以直接将'\'改为'/'即可,或者在含有转义字符的字符串前加'r'表示字符串不作转义处理。

5)文件对象方法
  1、f.read(size):读取文件内容,size是读取数据的数目,若不带参,则默认将文件的所有内容都读取并且返回。
  2、f.readline():从文件中读取单独的一行。换行符为 '\n'。f.readline() 如果返回一个空字符串, 说明已经已经读取到最后一行。
  3.f.readlines():将返回该文件中包含的所有行。如果设置可选参数sizehint, 则读取指定长度的字节, 并且将这些字节按行分割。
   或者以迭代访问的方式:
     f = open("D:/test.txt", "r")
     for line in f:
     print(line, end='')
     f.close()
  4、f.write(string) 将 string 写入到文件中, 然后返回写入的字符数。如果要写入一些不是字符串的东西,要先进行转换:
    eg:
      f = open("D:/test.txt", "r")
      value = ('www.runoob.com', 14)
      s = str(value)
      f.write(s)
      f.close()
  5、f.tell():返回文件对象当前所处的位置, 它是从文件开头开始算起的字节数。
  6、f.seek(offset,from_what)函数:改变文件当前的位置:from_what的值,如果是0表示开头,如果是1表示当前位置,2表示文件的结尾。
    seek(x,0):从起始位置即文件首行首字符开始移动 x 个字符:移动位数不包括自身位置起算:
    seek(x,1):表示从当前位置往后移动x个字符
    seek(-x,2):表示从文件的结尾往前移动x个字符:移动位数包括自身位置起算:
    如:(from_what 值为默认为0,即文件开头。)
      >>> f = open('D:/test.txt', 'rb+')
      >>> f.write(b'0123456789abcdef')#b应该是指代二进制的意思,不算在字符里面
      >>> f.seek(5) #输出:5
      >>> f.read(1) #从当前的位置输出一个字符,输出:b'5'
      >>> f.seek(-3, 2)#移动到文件的倒数第三字节,输出:13
      >>> f.read(1) #从当前的位置输出一个字符,输出:b'd'
  7、f.close():调用 f.close() 来关闭文件并释放系统的资源,如果尝试再调用该文件,则会抛出异常。

Guess you like

Origin www.cnblogs.com/yangrongkuan/p/12079616.html