1. csv文件
csv:Comma-Separated Values
半结构化数据
逗号分割值,可以使用office或者wps打开。
模块:csv
csv.reader(csvfile,dialect=’excel’,**fmtparams)
csv.writer(csvfile,dialect=’excel’,**fmtparams)
返回一个DictWriter实例。
主要支持的方法有:
writerow,写入一行
writerows,写入多行
手动写入csv文件:
1 | import csv |
['4', 'tom', '22', 'home']
['1', 'hello']
['22', 'aaaaaa']
2. ini文件
常见的配置文件格式
configparser 模块 ConfigParser 类
读
read(filenames,encoding=None)
sections()返回section列表
add_sections(section_name) 增加一个section
has_sectiones(section_name) 判断section是否存在
option(section) 返回section的所有option
has_option(section,option) 判断section是否存在这个option
get(section,option,*,raw=False,vars=None[,fallback])
从指定的断的选项上取值,如果找到就返回,如果没找到就去拿默认(default)配置信息。
items(section,raw=False,vars=None)
写:
set(section,option,value)
移除:
remove_section(section)
remove_option(section,option)
write(fileobject,space_around_delimiters=True)
将当前的config的所有内容写入fileobject中,一般open函数使用w模式,space_around_delimiters=True支持等号左右两侧有空格。
1 | from configparser import ConfigParser |
['test']
DEFAULT <Section: DEFAULT>
test <Section: test>
<class 'str'>
11
<class 'int'>
11