python抽取excel指定数据
时间: 2023-10-26 16:23:56 浏览: 161
要抽取Excel中的指定数据,可以使用以下方法:
首先,需要安装Excel读取数据的库xlrd。然后,获取Excel文件的位置并读取进来。接下来,读取指定的行和列的内容,并将其存储在列表中。
以下是完整的Python程序代码:
```python
import xlrd
from xlrd import xldate_as_tuple
import datetime
# 导入需要读取的Excel表格的路径
data1 = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test.xlsx')
table = data1.sheets()[0]
# 创建一个空列表,存储Excel的数据
tables = []
# 将Excel表格内容导入到tables列表中
def import_excel(excel):
for rown in range(excel.nrows):
array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''}
array['road_name'] = table.cell_value(rown,0)
array['bus_plate'] = table.cell_value(rown,1)
if table.cell(rown,2).ctype == 3:
date = xldate_as_tuple(table.cell(rown,2).value,0)
array['timeline'] = datetime.datetime(*date)
array['road_type'] = table.cell_value(rown,3)
array['site'] = table.cell_value(rown,4)
tables.append(array)
if __name__ == '__main__':
# 将Excel表格的内容导入到列表中
import_excel(table)
# 验证Excel文件存储到列表中的数据
for i in tables:
print(i)
```
在上述代码中,我们首先导入需要读取的Excel表格的路径,然后创建一个空列表`tables`来存储Excel的数据。接下来,我们定义了一个函数`import_excel`,用于将Excel表格内容导入到`tables`列表中。最后,在`if __name__ == '__main__'`的代码块中,我们调用`import_excel`函数将Excel表格的内容导入到列表中,并打印出验证结果。
注意,你需要将代码中的Excel文件路径修改为你实际的文件路径。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [python如何读取excel表数据](https://blog.csdn.net/ekcchina/article/details/124446961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文
相关推荐
















