<pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
    <pre id="vvttv"></pre>

      <p id="vvttv"></p>

          <p id="vvttv"></p>

                <p id="vvttv"></p>

                <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                  <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                    <p id="vvttv"></p>

                    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 ?????????????????е???????????з??????б?????????????????Ч??????????????????????????????????檔???????????????????С?

                    >>> 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?????????????????????????????????????????鼼????????????????? 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

                      <pre id="vvttv"><mark id="vvttv"><progress id="vvttv"></progress></mark></pre>
                      <pre id="vvttv"></pre>

                        <p id="vvttv"></p>

                            <p id="vvttv"></p>

                                  <p id="vvttv"></p>

                                  <pre id="vvttv"><cite id="vvttv"><progress id="vvttv"></progress></cite></pre>

                                    <output id="vvttv"><dfn id="vvttv"><th id="vvttv"></th></dfn></output>

                                      <p id="vvttv"></p>

                                      这里只有精品视频