python学习之输出与文件读写

11/24/2015来源:python人气:796


#1. 打印字符串
PRint ("His name is %s"%("Aviad"))

#2.打印整数
print ("He is %d years old"%(25))

#3.打印浮点数
print ("His height is %f m"%(1.83))

#4.打印浮点数(指定保留小数点位数)
print ("His height is %.2f m"%(1.83))

#5.指定占位符宽度
print ("Name:%-10s Age:%8d Height:%8.2f"%("Aviad",25,1.83))

#6.指定占位符宽度(左对齐)
print ("Name:%-10s Age:%-8d Height:%-8.2f"%("Aviad",25,1.83))

#7.指定占位符(只能用0当占位符?)
print ("Name:%-10s Age:%08d Height:%08.2f"%("Aviad",25,1.83))

 

按一定格式或模板占位符,打印出来

t=12,34,"avbcdf";

m,n,s=t;


print(m,n,s);#print(n); print(s);

print("m is {0} and n is {1} and s is {2}".format(m,n,s));

print("m is {} and n is {} and s is {}".format(m,n,s));

print("m is {m} and n is {n} and s is {s}".format(m=2,n=3,s=9));

print("m is {} and n is {} and s is {s}".format(2,3,s=9));

import math;
print("{0:.3f}".format(math.pi));


table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d};Dcab: {0[Dcab]:d}'.format(table))

for name,number in table.items():
    print("name is {0} and number is{1:10d}".format(name,number));




#json 序列化
import json;
js=json.dumps([1, 'simple', 'list']);
print(js);
print(type(js));


arra=[1, 'simple', 'list'];
path="D:\\a.txt";

#r+表示同时具备读写
f = open(path, 'r+')
f.write('01wwwfefefef234fdfsadfsfsd56789abcdef')

#将序列化写时文件中
json.dump(arra,f);

 

#json反列化
arra_new=json.load(f);
print(arra_new);
print(type(arra_new));