<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    Previous Page

    Up One Level

    Next Page

    Python ???

    Contents

    ????? 6. ??? ????? Python ??? ???8. ???????


    ?????



     
    7.
    ????????

    ?技?????????????????????????????????????????????????忱????????????芍?????????????????快???????

     
    7.1
    ?????????

    ??????????????????????????????????print???????????????????????????wite()???????????????????羊?sys.stdout?????????米???羊??????

    ??????????????????????宏????????????????????????????????????????????????????????????????????????????????????????????????????????百?????????????????????? string ???????宏????????????????????????????宏?????????芍??????????????????????????????????? % ?????????????????????????????? % ???????????????????????? sprintf()??????????????????????????????辰????戒????????????????

    ??????????????????糸????????????????????????????Python?????????????? repr() ?? str() ??????????????????????????''???????repr???????????????????芍?

    ????str() ???????????????????????????? repr()???????????????????????????快????????????SyntaxError ???? ??????????????????????????????str()??????repr()????????????????????????????????????????????????????????????????????????????????????????????????

    ???????宏?????

    >>> s = 'Hello, world.'
    >>> str(s)
    'Hello, world.'
    >>> repr(s)
    "'Hello, world.'"
    >>> str(0.1)
    '0.1'
    >>> repr(0.1)
    '0.10000000000000001'
    >>> x = 10 * 3.25
    >>> y = 200 * 200
    >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
    >>> print s
    The value of x is 32.5, and y is 40000...
    >>> # The repr() of a string adds string quotes and backslashes:
    ... hello = 'hello, world\n'
    >>> hellos = repr(hello)
    >>> print hellos
    'hello, world\n'
    >>> # The argument to repr() may be any Python object:
    ... repr((x, y, ('spam', 'eggs')))
    "(32.5, 40000, ('spam', 'eggs'))"
    >>> # reverse quotes are convenient in interactive sessions:
    ... `x, y, ('spam', 'eggs')`
    "(32.5, 40000, ('spam', 'eggs'))"

    ??????????????????????????????

    >>> import string
    >>> for x in range(1, 11):
    ...     print string.rjust(repr(x), 2), string.rjust(repr(x*x), 3),
    ...     # Note trailing comma on previous line
    ...     print string.rjust(repr(x*x*x), 4)
    ...
     1   1    1
     2   4    8
     3   9   27
     4  16   64
     5  25  125
     6  36  216
     7  49  343
     8  64  512
     9  81  729
    10 100 1000
    >>> for x in range(1,11):
    ...     print '%2d %3d %4d' % (x, x*x, x*x*x)
    ... 
     1   1    1
     2   4    8
     3   9   27
     4  16   64
     5  25  125
     6  36  216
     7  49  343
     8  64  512
     9  81  729
    10 100 1000

    ??????????????print????????????????????????????????????????????

    ?????????string.rjust()????????????????????????????????孝?????????????????????????????????????? string.ljust() ?? string.center()????宏???????????米??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????鉸"string.ljust(x, n)[0:n]"????

    ?????????????string.zfill()???????????????????????????0???迆????????????????????

    >>> import string
    >>> string.zfill('12', 5)
    '00012'
    >>> string.zfill('-3.14', 7)
    '-003.14'
    >>> string.zfill('3.14159265359', 5)
    '3.14159265359'

    ??????????????? % ????????

    >>> import math
    >>> print 'The value of PI is approximately %5.3f.' % math.pi
    The value of PI is approximately 3.142.

    ????忍???????????????????????????????????????????????????????????

    >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
    
    >>> for name, phone in table.items():
    ...     print '%-10s ==> %10d' % (name, phone)
    ... 
    Jack       ==>       4098
    Dcab       ==>       7678
    Sjoerd     ==>       4127

    ???????C????????????????????????????????????????忪??????????????????????????????????????however, if you don't you get an exception, not a core dump?? ??? %s ??????????宏???????????????????????????????????? str() ???????????????Python????? * ????????????????????????????????Python?????C?? %n ?? %p ????????

    ??????????????????????????????????????????????????????????????????????????完???????????? form %(name)????????

    >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
    >>> print 'Jack: %(Jack)d; Sjoerd: %(Sjoerd)d; Dcab: %(Dcab)d' % table
    Jack: 4098; Sjoerd: 4127; Dcab: 8637678

    ????????????米????迆??? vars() ????????????????迆?????????????????抉???????????

     
    7.2
    ??忱???

    open()  ?????????????? ????????‾??????????????“open(filename, mode)??

    >>> f=open('/tmp/workfile', 'w')
    >>> print f
    <open file '/tmp/workfile', mode 'w' at 80a0960>

    ?????????????????????????????????????????????????????????????????????????????????芍?????????孝?‘r'????????????????‘w’????????????忱???????????????辰??????????????????? ‘a’?????????????????????‘r+’??????????忱?????????????????????????‘r’????

    ??Windows ?? Macintosh?????‘b’??????????????????????????????????‘rb’??‘wb’??‘r+b’????????? Windows????????????????????????????????忱???????????汕?????????扶??????? ?????????????????????????????????????JPEG??EXE?????????????????????????????????宏???????????????????????????????????Mactiontosh????????????????????????C????

     
    7.2.1
    ???????file object???????

    ?????快????????????????f?????????

    ??????????????????? f.read(size)???‾??????????????????????????????????????????????????????size????????妊???????????size?????????????????????????????????????????妊????????????????????????????????????㏒???size???????????????????????????????汕??f.read() ????????????????""????

    >>> f.read()
    'This is the entire file.\n'
    >>> f.read()
    ''

    f.readline() ??????忪????????孝????????汕???????????????戒?????快?????????????????戒???汕?????????????????????????????????????????????? if f.readline() ???????????????????????????????汕?????????????孝?????????‘\n’?????????????戒??????????

    >>> f.readline()
    'This is the first line of the file.\n'
    >>> f.readline()
    'Second line of the file\n'
    >>> f.readline()
    ''

    f.readlines() ????????忌?????忘?????????????快??????妊?????????? sizehint ?????????????????快???????????戒??????忌?????????????????完??????????????????????????????????n???????????????????妊?

    >>> f.readlines()
    ['This is the first line of the file.\n', 'Second line of the file\n']

    f.write(string) ?? string ??????忱???????????None??

    >>> f.write('This is a test\n')

    f.tell() ??????????????????????????????快????竹???????????????????????????????????????????????????????????“f.seek(offset, from_what)???????辰????忱??????????竹????? offset ?????????竹???? from_what ?????????. from_what ??0??????????????????1?????????????竹??????2?????????汕????? from_what ???????????????????????????????

    >>> f=open('/tmp/workfile', 'r+')
    >>> f.write('0123456789abcdef')
    >>> f.seek(5)     # Go to the 6th byte in the file
    >>> f.read(1)        
    '5'
    >>> f.seek(-3, 2) # Go to the 3rd byte before the end
    >>> f.read(1)
    'd'

    ????????????? f.close() ??????????????????????????????????? f.close()???????????????????????????

    >>> f.close()
    >>> f.read()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: I/O operation on closed file

    ??????????宏???????????????????? isatty() ?? truncate() ???羊????????????????????????

     
    7.2.2 pickle
    ???

     

    ??????????????忱????快???????????????????????????read() ??????????????????y?????string.atoi()?????孝???????'123' ????????????????????????????????????????????????????????????????????????????????????????

    ????????????????????忱???????褡??????????????Python?????????? Pickle ??????????????????????????????????百?Python???????????宏Python????form??????????????????????????????? ??pickling??????????????????1??????????????unpickling??????????快??????????????????孝???????????????????????????

    ??????????????x???????忱?????????????f????????????????????????忱???

    pickle.dump(x, f)

    ???f???????????????????????????????????????

    x = pickle.load(f)

    ??????????????????忱??????????????宏?????????芍???????pickle????????羊??????

    pickle ??Python?????????????????????????????????????????G????????????????? persistent object ??????? pickle ?????????????Python???????????????????????????????????????????????????


    Previous Page

    Up One Level

    Next Page

    Python ???

    Contents

    ????? 6. ??? ????? Python ??? ???8. ???????


    Release 2.3, documentation updated on July 29, 2003.

    See About this document... for information on suggesting changes.
    ?????, @ssv 97av