本文介绍了我将如何使用Excel文件并将其列转换成Python中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题。也就是说,假设我有一个excel文件,在第一行有一些名字。然后下一个,但是很多行是单字母条目(因此A,B,C等)。



我的目标是提取列并将其列入列表,因此,对于列1,列表将从第2行开始,下一个条目将是从第3行开始,直到结束。



我将如何在Python中执行此操作?

解决方案

我想出了答案。假设Excel文件是 Test.xlsx ,我们可以将第j列(不包括第一行)转换为list_j,如下所示:

  book = xlrd.open_workbook(Test.xlsx)
sheet = book.sheet_by_index(0)

list_j = []

for k in range(1,sheet.nrows):
list_j.append(str(sheet.row_values(k)[j-1]))
/ pre>

I had the following question. Namely, suppose I have an excel file with some names in the first row. Then the next however many rows are single letter entries (so "A", "B", "C" and so on).

My goal is to extract the column and make it into a list, so that, say, for column 1 the list would start in row 2, the next entry would be from row 3, and so on, until the end is reached.

How would I do that in Python?

解决方案

I figured out the answer. Assuming the Excel file is Test.xlsx, we can translate the j-th column (excluding the first row) into list_j as follows:

book = xlrd.open_workbook("Test.xlsx")
sheet = book.sheet_by_index(0)

list_j = []

for k in range(1,sheet.nrows):
    list_j.append(str(sheet.row_values(k)[j-1]))

这篇关于我将如何使用Excel文件并将其列转换成Python中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:13