deftxt_writer():'''write file'''withopen('data.txt','w',encoding='utf-8')as f:
f.write('class')
lines =['address:Beijing\n','qq:87657658\n','website:http://uke.cc']
f.writelines(lines)deftxt_read():'''read file'''withopen('data.txt',encoding='utf-8')as f:for line in f:print(line,end='')if __name__ =='__main__':#txt_writer()
txt_read()
2. csv
读取csv
import csv
from collections import namedtuple
defcsv_read():'''csv basic read'''withopen('product.csv',encoding='utf8')as f:
reader = csv.reader(f)
headers =next(reader)#遍历第一行print(headers)for row in reader:print('order:{}\tproduct:{}\tprice:{}'.format(row[0],row[1],row[3]))defcsv_read_by_namedtuple():'''read csv and use namedtuple representing the name of column'''withopen('product.csv',encoding='utf8')as f:
reader = csv.reader(f)
headers =