Ai
1 Star 1 Fork 1

luohanye/python-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
readCensusExcel.py 960 Bytes
一键复制 编辑 原始数据 按行查看 历史
luohanye 提交于 2019-08-25 00:03 +08:00 . 读取excel
#! python3
# readCensusExcel.py - Tabulates population and number of census tracts for
# each county.
import pprint, openpyxl
print('打开工作簿...')
wb = openpyxl.load_workbook('resource/censuspopdata.xlsx')
sheet = wb['Population by Census Tract']
countyData = {}
# 填充
for row in range(2, sheet.max_row+1):
state = sheet['B' + str(row)].value
county = sheet['C' + str(row)].value
pop = sheet['D' + str(row)].value
# 确保州存在
countyData.setdefault(state, {})
# 确保县在州中存在
countyData[state].setdefault(county, {'tracts': 0, 'pop': 0})
# 每一行代表一个县某统计区的人口数
countyData[state][county]['tracts'] += 1
# 追加县的人口数
countyData[state][county]['pop'] += int(pop)
# 将结果写入文件
print('写入结果...')
resultFile = open('census2010.py', 'w')
resultFile.write('allData = ' + pprint.pformat(countyData))
resultFile.close()
print('完成.')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/indexman/python-core.git
git@gitee.com:indexman/python-core.git
indexman
python-core
python-core
master

搜索帮助