#! usr/bin/env python
# _*_ coding:utf-8 _*_
'''
一、格式化输出
1、str.format()
a、位置参数、关键字参数
b、在字段后的 ':' 后面加一个整数会限定该字段的最小宽度,这在美化表格时很有用:
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print('{0:10} ==> {1:10d}'.format(name, phone))
...
Jack ==> 4098
Dcab ==> 7678
Sjoerd ==> 4127
c、用 ‘**’ 标志将这个字典以关键字参数的方式传入:
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
2、旧式字符串格式化:%
二、文件读写
1、函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode)。
第一个参数是一个含有文件名的字符串。第二个参数也是一个字符串,含有描述如何使用该文件的几个字符。
mode 为 'r' 时表示只是读取文件;
'w' 表示只是写入文件(已经存在的同名文件将被删掉);
'a' 表示打开文件进行追加,写入到文件中的任何数据将自动添加到末尾。
'r+' 表示打开文件进行读取和写入。对于同一个文件会进行数据从头开始覆盖;
mode 参数是可选的,默认为 'r'。
2、读取形式
f.read()
f.readline():从文件中读取单独一行
for i in file:直接读取
如果你想把文件中的所有行读到一个列表中,你也可以使用 list(f) 或者 f.readlines()。
用关键字 with 处理文件对象是个好习惯。它的先进之处在于文件用完后会自动关闭,就算发生异常也没关系。它是 try-finally 块的简写。
三、使用json存储结构化数据
1、序列化: json.dumps()、json.dump()
2、反序列化:json.loads(),json.load()
其中:如果 f 是为写入而打开的一个 文件对象。则可以使用:json.dump(list1,open('testfile.txt','w'))
如果 f 是为读取而打开的 文件对象,则可以使用:
'''
#使用位置参数进行格式化输出
print('What is your {0}? my name is {1}!'.format('name','han mei mei'))
#使用关键字参数进行格式化输出
print('This {animal} is eating {food}.'.format(animal='dog',food='bone'))
#位置参数和关键字参数进行混用
print('What is your {0}? my name is {1}!This {animal} is eating {food}.'.format('name','li lei',animal='dog',food='bone'))
#在字段后的 ':' 后面加一个整数会限定该字段的最小宽度,这在美化表格时很有用:
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name,number in table.items():
print('{0:10} ==> {1:10d}'.format(name,number))
for x in range(1,11):
print('{0:2d} {1:3d} {2:4d}'.format(x,x*x,x*x*x))
#用 ‘**’ 标志将这个字典以关键字参数的方式传入:
# print(**table)
# print('jack:{jack:d};Sjoerd:{Sjoerd:d};Dcab:{Dcab:d}'.format(**table))
#旧式的字符串格式化 %
import math
print('The value of PI is %5.3f' % math.pi)
#read()读取文件
# f = open('testfile.txt','r')
# print(f.read())
# f.close()
#使用readline()读取
# print(f.readline())
# f.close()
# for line in f:
# print(line)
#print(f.readlines())
#print(list(f))
# for line in f.readlines():
# print(line)
# with open('testfile.txt','r+') as f:
# f.write('changlin test')
#json存储结构化数据
#读取python对象为字符串形式
import json
'''
list1 = [1,'sample','list']
str1 = json.dumps(list1)
print(str1)
str2 = json.dump(list1,open('testfile.txt','w')) #覆盖整个文件内容
listDemo = json.load(open('testfile.txt','r'))
print(listDemo)
print(type(listDemo))
'''
#python中的json模块可以将dict转str,str转dict,dict存储到文件中,文件中导入成dict
dict1={'user':'john','sex':'male'}
str1=json.dumps(dcit1)#<class 'str'>
print(json.loads(str1))#{'user': 'john', 'sex': 'male'}
#可以直接用字符串转换
str2='{"user":"john","sex":"male"}'
print(json.loads(str2))#{'user': 'john', 'sex': 'male'}
#导出和导入文件的代码
str='{"user":"john","sex":"male"}'
with open('test.txt','w',encoding='utf-8') as fout:
json.dump(str,fout)
with open('test.txt','r',encoding='utf-8') as fout:
print(json.load(fout))
#json还可以将dict的list转成str存到文件里,再用load可以读入成一个dict格式的list
with open('test.txt','w',encoding='utf-8') as f:
f.writelines(json.dumps([{'hello':'world'},{'hello':'world'},{'hello':'world'}]))
print(type(json.dumps([{'hello':'world'},{'hello':'world'},{'hello':'world'}])))
with open('test.txt','r',encoding='utf-8') as f:
test_l=json.load(f)
print(type(test_l[0]))