学习目标:
做个简单程序方便自己查找相同数据并记录到地籍信息当中
学习内容:
# -*- coding: utf-8 -*-
import xlrd
from datetime import date,datetime
def read_excel():
#'''-------------------------------------------------1文件导入-------------------------------------------------------------------'''
# 打开1文件
workbook = xlrd.open_workbook(r'C:\Users\dong bo\PycharmProjects\pythonProject2\dongfeng.xls')
# 获取所有sheet
sheet_name = workbook.sheet_names()[1]
# 根据sheet索引或者名称获取sheet内容
#sheet = workbook.sheet_by_index(0) # sheet索引从0开始
sheet = workbook.sheet_by_name('东风村明细表')
print (workbook.sheets()[0])
# sheet的名称,行数,列数
print (sheet.name,sheet.nrows,sheet.ncols)
# 获取整列的值(数组)
cols0 = sheet.col_values(2) # 获取第3列内容
# 删除中间空格
while '' in cols0:
cols0.remove('')
cols0.remove(cols0[0])
print(type(cols0))
print(cols0)
# '''-------------------------------------------------2文件导入-------------------------------------------------------------------'''
#使用笨方法,再次导入另一个excel表
# 图导出文件
contractworkbook = xlrd.open_workbook(r'C:\Users\dong bo\PycharmProjects\pythonProject2\册底塘2.xls')
# 获取所有sheet
sheet_name = contractworkbook.sheet_names()[0]
print(sheet_name)
# 根据sheet索引或者名称获取sheet内容
# sheet = workbook.sheet_by_index(0) # sheet索引从0开始
othersheet = contractworkbook.sheet_by_name('ZD_宗地基本信息属性表')
print(contractworkbook.sheets()[0])
# sheet的名称,行数,列数
print(othersheet.name, othersheet.nrows, othersheet.ncols)
# 获取整行和整列的值(数组)
cols1 = othersheet.col_values(4) # 获取第3列内容
# 删除中间空格
while '' in cols1:
cols1.remove('')
cols1.remove(cols1[0])
print(type(cols1))
print(cols1)
# '''-------------------------------------------------比较查询-------------------------------------------------------------------'''
# 文件的列表cols0与图导出文件的列表cols1取交集。
lst = []
for i in cols0:
if i in cols1:
lst.append(i)
print(lst)
if __name__ == '__main__':
# 读取Excel
read_excel();
print ('读取成功')
=