资源简介
条件:已知现状OD表,将来OD表各行、各列总和。通过程序实现对分布交通流的计算,能实现两种计算方式:1.平均增长系数法;2.底特律法。
代码片段和文件信息
from win32com.shell import shell
from win32com.shell import shellcon
import xlrd
import xlwt
# 获取桌面路径函数
def GET_DTPATH():
ilist = shell.SHGetSpecialFolderLocation(0 shellcon.CSIDL_DESKTOP)
dtpath = shell.SHGetPathFromIDList(ilist)
dtpath = dtpath.decode(“gbk“)
return dtpath
# 打开“OD”文工作簿函数
def open_OD():
# 获取路径
open_path1 = GET_DTPATH() + “\OD.xlsx“
open_path2 = GET_DTPATH() + “\OD.xls“
# 打开文件
try:
OD = xlrd.open_workbook(open_path1)
except:
try:
OD = xlrd.open_workbook(open_path2)
except:
print(“在桌面找不到命名为“OD”的表格文件。。。“)
input()
return OD
# 行求和函数
def SUM_ROW(arry):
sum_row = []
for i in range(len(arry)):
row = 0
for j in range(len(arry[0])):
row = row + arry[i][j]
sum_row.append(row)
return sum_row
# 列求和函数
def SUM_COL(arry):
sum_col = []
for i in range(len(arry[0])):
col = 0
for j in range(len(arry)):
col = col + arry[j][i]
sum_col.append(col)
return sum_col
# 将表格数据储存为列表数据
def excel_To_list():
# 打开“OD”文件
OD = open_OD()
# 获取表格1,表格2
now_od_sheet = OD.sheets()[0]
future_od_sheet = OD.sheets()[1]
# 现状OD表
now_od = []
# 将来OD表
future_od = []
# 获取现状OD表数据,保存成列表
for i in range(now_od_sheet.nrows):
now_od.append(now_od_sheet.row_values(i))
# 获取将来OD表数据,保存成列表
for i in range(future_od_sheet.nrows):
future_od.append(future_od_sheet.row_values(i))
return now_od future_od
print(“现在OD表:“ now_od)
print(“将来OD表::“ future_od)
# 将计算结果写入excel表格
def list_To_excel(q O D n file_name):
workbook = xlwt.Workbook(encoding=“asscii“)
# 写入
for n in range(1 n + 1):
worksheet = workbook.add_sheet(“第“ + str(n) + “次计算“)
# 写入OD细节值
for i in range(len(q[n])):
for j in range(len(q[n][0])):
worksheet.write(i + 1 j + 1 q[n][i][j])
# 写入“合计“字样
worksheet.write(0 len(q[n][0]) + 1 “合计“)
worksheet.write(len(q[n]) + 1 0 “合计“)
# 写入行求和
for i in range(1 len(q[n]) + 1):
worksheet.write(i len(q[n][0]) + 1 O[n][i - 1])
worksheet.write(i 0 i)
# 写入列求和
for j in range(1 len(q[n][0]) + 1):
worksheet.write(len(q[n]) + 1 j D[n][j - 1])
worksheet.write(0 j j)
# 保存路径
save_path = GET_DTPATH() + “\\“ + file_name + “.xls“
try:
workbook.save(save_path)
print(“\n\n保存成功....请查看桌面文件:“ + file_name + “.xls“)
except:
print(“\n\n保存不成功.....可能该文件正在打开状态“)
# 显示信息
print(“1.平均增长系数法 2.底特律法“)
which_way = input(“输入选择计算的方法:“)
# 检验标准
a = 0.03
try:
if which_way == “1“:
print(“+------------------------+“)
print(“| 平均增长系数法 |“)
print(“+------------------------+“)
file_name = “平
- 上一篇:python 爬取图片
- 下一篇:python3.dll——python3.5.2
评论
共有 条评论