1.重新格式化数据并写入到CSV文件中
#!/usr/bin/python3
#coding:utf-8
#重新格式化数据(日期)并写入到CSV文件
import csv
from datetime import datetime
def convertDate(item):
theDate = item[-1] #从记录中提取日期域
dateObj = datetime.strptime(theDate,'%Y-%m-%d') #解析(parse)日期域
dateStr = datetime.strftime(dateObj,'%m/%d/%Y') #格式化(format)日期对象
item[-1] = dateStr
return item
if __name__ == '__main__':
with open('ToolhireData/tooldesc.csv') as td: #打开原始CSV文件
rdr = csv.reader(td)
items = list(rdr) #读取记录并保存在items的列表
items = [convertDate(item) for item in items]
with open("./ToolhireData/tooldesc2.csv",'w',newline = '') as td:
wrt = csv.writer(td)
for item in items:
wrt.writerow(item)
2.添加标题行(键值)到CSV文件中
#!/usr/bin/python3
#coding:utf-8
#将标题行(键值)添加到CSV文件
#DicReader 返回字典dict 而reader 返回列表list
import csv
fields = ['ItemID','Name','Description','Owner',
'Price','Condition','DateRegistered']
with open('ToolhireData/tooldesc2.csv') as td_in:
rdr = csv.DictReader(td_in,fieldnames = fields) #初始化fieldnames
items = [item for item in rdr]
with open('ToolhireData/tooldesc3.csv','w',newline = '') as td_out:
wrt = csv.DictWriter(td_out,fieldnames = fields)#通过fieldnames指定了域的顺序(字典保存的域是无序的)
wrt.writeheader()
wrt.writerows(items) #将整个items列表一次写出
3.使用ElementTree 解析 XML
#!/usr/bin/python3
#coding:utf-8
#使用ElementTree 解析 XML 文件
#提取日期并计算书本的平均借出时间
import xml.etree.ElementTree as ET #导入ElementTree解析器
import datetime as dt
def parseDates(filename):
dates = []
rows = []
dom = ET.parse(filename) #文件解析
#root = dom.getroot() #得到文件根结点
for node in dom.iter('*'): #查找文件中所有内容 ‘*’通配符
if 'Row' in node.tag:
rows.append(node) #结点列表
#检查结点列表中的每一行#
for row in rows:
row_dates = []
for node in row.iter('*'):
for key,value in node.attrib.items(): #检查特性key为‘Type’,value为'DateTime'的行
if 'Type' in key and 'DateTime' in value:
row_dates.append(node.text)
if len(row_dates) == 2: #如果row_dates列表包含两个日期(借书日期,还书日期),则添加到dates列表
dates += row_dates
return dates
def calculateAverage(dates):
loan_periods = []
while dates:
lent = dates.pop(0).split('T')[0]
ret = dates.pop(0).split('T')[0]
lent_date = dt.datetime.strptime(lent,'%Y-%m-%d')
ret_date = dt.datetime.strptime(ret,'%Y-%m-%d')
loan_periods.append( (ret_date - lent_date).days )
average = sum(loan_periods)/len(loan_periods)
return average
if __name__ == '__main__':
dates = parseDates('toolhire.xml')
avg = calculateAverage(dates)
print('Average loan period is: {} days'.format(avg))