YAML语法规则: http://www.ibm.com/developerworks/cn/xml/x-cn-yamlintro/ 下载PyYAML: http://www.yaml.org/ 解压安装: python setup.py install 1.新建test.yaml文件,内容如下: name: Tom Smith age: 37 spouse: name: Jane Smith age: 25 children: - name: Jimmy Smith age: 15 - nam
with open('data.txt','w') as f: #设置文件对象 w是重新写,原来的会被抹掉,a+是在原来的基础上写 str0=u"写文件\n" #写中文要在字符串签名加个u来表示Unicode str1="I am good kid who like study" f.write(str0) f.write(str1) 文件内容: 写文件I am good kid who like study 注:人生苦短,我用python
f是指向文件的指针,r是转义字符,可以让字符串中的\保持不被转义.路径点属性查然后加上当前文件. 'w'表示只写,‘r’表示只读. import random 导入random数 s = []开一个空列表 循环,2^20用2**20表示 append是apply to end 把字符串接到后面 s = ''.join(s)表示以''中的元素为间隔插入字符串 f.write(s)写入s到文件中 f.close表示关闭文件并保存,无这句则未保存 这是生成的那个文件 s.close后打开文件发现测试数
import yaml with open("./test.yaml") as f: x = yaml.load(f) print(x) [{'tasks': [{'yum': {'state': 'latest', 'name': 'httpd'}, 'name': 'ensure apache is at the latest version'}, {'name': 'write the apache config file', 'template': {'dest': '/etc
name=['lucy','jacky','eric','man','san'] place=['chongqing','guangzhou','beijing','shanghai','shenzhen'] with open('Agoly.csv','w') as file: file.write('name,place\n') for n,p in zip(name,place): file.write("{},{}\n".format(n,p))