Python 在 openpyxl 中查看行值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31236998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
View row values in openpyxl
提问by dyao
In the csv module in python, there is a function called csv.reader
which allows you to iterate over a row, returns a reader object and can be held in a container like a list.
在 python 的 csv 模块中,有一个被调用的函数csv.reader
,它允许您迭代一行,返回一个读取器对象,并且可以像列表一样保存在容器中。
So when the list assigned to a variable and is printed, ie:
因此,当列表分配给变量并打印时,即:
csv_rows = list(csv.reader(csvfile, delimiter=',', quotechar='|'))
print (csv_rows)
>
>
>
[['First Name', 'Last Name', 'Zodicac', 'Date of birth', 'Sex'] # I gave an example of the function outputting a header row
So far, I don't see a similar function like this in the openpyxl. I could be mistaken so I'm wondering if any of you can help me out.
到目前为止,我在 openpyxl 中没有看到类似的功能。我可能弄错了,所以我想知道你们中是否有人可以帮助我。
Update
更新
@alecxe, your solution works perfectly (except its casting my date of birth as a datetime format instead of a regular string).
@alecxe,您的解决方案完美运行(除了将我的出生日期转换为日期时间格式而不是常规字符串)。
def iter_rows(ws):
for row in ws.iter_rows():
yield [cell.value for cell in row]
>
>
>>> pprint(list(iter_rows(ws)))
[['First Nam', 'Last Name', 'Zodicac', 'Date of birth', 'Sex'], ['John', 'Smith', 'Snake', datetime.datetime(1989, 9, 4, 0, 0), 'M']]
Since I'm a beginner I wanted to know how this would work if I used a for loop instead of a list comprehension.
由于我是初学者,我想知道如果我使用 for 循环而不是列表理解,这将如何工作。
So I used this:
所以我用了这个:
def iter_rows(ws):
result=[]
for row in ws.iter_rows()
for cell in row:
result.append(cell.value)
yield result
It almostgives me the exact same output, instead it gives me this: As you can tell, it essentially gives me one gigantic list instead of nested list in the result you gave me.
它几乎给了我完全相同的输出,相反它给了我这个:正如你所知,它本质上给了我一个巨大的列表,而不是你给我的结果中的嵌套列表。
>>>print(list(iter_rows(ws)))
[['First Nam', 'Last Name', 'Zodicac', 'Date of birth', 'Sex', 'David', 'Yao', 'Snake', datetime.datetime(1989, 9, 4, 0, 0), 'M']]
采纳答案by alecxe
iter_rows()
has probably a similar sense:
iter_rows()
可能有类似的意义:
Returns a squared range based on the range_string parameter, using generators. If no range is passed, will iterate over all cells in the worksheet
使用生成器返回基于 range_string 参数的平方范围。如果没有传递范围,将遍历工作表中的所有单元格
>>> from openpyxl import load_workbook
>>>
>>> wb = load_workbook('test.xlsx')
>>> ws = wb.get_sheet_by_name('Sheet1')
>>>
>>> pprint(list(ws.iter_rows()))
[(<Cell Sheet1.A1>,
<Cell Sheet1.B1>,
<Cell Sheet1.C1>,
<Cell Sheet1.D1>,
<Cell Sheet1.E1>),
(<Cell Sheet1.A2>,
<Cell Sheet1.B2>,
<Cell Sheet1.C2>,
<Cell Sheet1.D2>,
<Cell Sheet1.E2>),
(<Cell Sheet1.A3>,
<Cell Sheet1.B3>,
<Cell Sheet1.C3>,
<Cell Sheet1.D3>,
<Cell Sheet1.E3>)]
You can modify it a little bit to yield a list of row values, for example:
您可以稍微修改它以生成行值列表,例如:
def iter_rows(ws):
for row in ws.iter_rows():
yield [cell.value for cell in row]
Demo:
演示:
>>> pprint(list(iter_rows(ws)))
[[1.0, 1.0, 1.0, None, None],
[2.0, 2.0, 2.0, None, None],
[3.0, 3.0, 3.0, None, None]]
回答by Joseph Zullo
I got it to work using this method:
我使用这种方法让它工作:
all_rows = []
for row in worksheet:
current_row = []
for cell in row:
current_row.append(cell.value)
all_rows.append(current_row)
Essentially, I created a list for all of the data.
Then, I iterated through each row in the worksheet.
Each cell.value
within a row was added to a short-term list (current row).
Once all of the cell.values
within the row are added to the short-term list, the short-term list is added to the long-term list.
本质上,我为所有数据创建了一个列表。然后,我遍历工作表中的每一行。cell.value
一行中的每一个都被添加到一个短期列表(当前行)中。一旦cell.values
该行内的所有内容都添加到短期列表中,则短期列表将添加到长期列表中。