简体   繁体   English

Python(xlrd):如何从Excel中获取多个特定列的值?

[英]Python (xlrd): How to get values of multiple specific columns from an Excel?

Currently I am using this to read the entire row: 目前,我正在用它来读取整行:

wr_stats.writerow(workbook.sheet_by_name('count').row_values(rownum))

Now I want to get only the values of multiple specific columns, preferably in the order of my choice. 现在,我只想获取多个特定列的值,最好按我选择的顺序。 I tried: 我试过了:

wr_stats.writerow(workbook.sheet_by_name('count').row_slice(rownum, start_colx=0, end_colx=2))

but that prints the data type along with the values. 但这会打印数据类型和值。 Instead of "3", I get "number:3". 我得到的不是“ 3”,而是“ number:3”。

Append .value to the cell you are reading, but I am not sure if this works with a slice. .value附加到您正在读取的单元格上,但是我不确定这是否适用于切片。

http://xlrd.readthedocs.io/en/latest/api.html#xlrd.sheet.Cell http://xlrd.readthedocs.io/zh-CN/latest/api.html#xlrd.sheet.Cell

By the way, if you are using Excel 2010+ you should look into https://openpyxl.readthedocs.io/en/stable/ 顺便说一句,如果您使用的是Excel 2010+,则应查看https://openpyxl.readthedocs.io/en/stable/

From there you can use a list comprehension: 从那里您可以使用列表理解:

wr_stats.writerow([x.value for x in workbook.sheet_by_name('count').row_slice(rownum, start_colx=0, end_colx=2)])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM