您的位置:首页 > 编程语言 > Python开发

Python input and output

2014-12-26 16:32 218 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: