Python input and output

来源:互联网 发布:js面向对象高级编程 编辑:程序博客网 时间:2024/05/16 17:45

1.  output   a. string   b. %=>like printf

2. str(make a string) or repr(convert for interpreter)

    'the value is x = '  + repr(x) + 'the value is y = ' + repr(y)

    The repr() of a string adds string quotes and backslashes

    The argument to repr() may be any Python object:

    hello = 'hello,world\n'

    hellos = repr(hello)

    print hellos

    import string

    for x in range(1,11):

          print string.rjust(repr(x),2),string.rjust(repr(x*x),3),

          print string.rjust(repr(x*x*x),5)      # similar function ljust(),center()

   string.zfill('123',5) #00123

   string.zfill('-123',5) #-00123

   print '%5.3s'%math.pi # comma take one position

   print 'jack:%(jack)d;Sjoerd:%(addf)d;Dcab:%(Dcab)d'%table  # return a locally dictionary

3. f = open('path','w') # w only write, r only write, r+ read and write, a write at the end

    print f

    f.read(size)

    f.readline()  

    f.readlines() # return a list

    f.write(string)

    f = open('path','r+')

    f.write('0123456789')

    f.seek(5) # go to 6th byte in the file

    f.read(1) # read one element

    f.tell() # the index

    f.seek(offset,from_what) 0 begin,1current,2 end,default is 0

    f.close()

4. pickle module

    pickling object       pickle.dump(x,f)

    unpickling object   x = pickle.load(f)

 

  

  

0 0
原创粉丝点击