查看openpyxl中的行值 [英] View row values in openpyxl

查看:511
本文介绍了查看openpyxl中的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python的csv模块中,有一个名为csv.reader的函数,该函数可让您遍历一行,返回阅读器对象,并可以保存在列表之类的容器中.

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.

所以当分配给变量的列表被打印时,即:

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

到目前为止,我在openpyxl中没有看到类似的功能.我可能会误会,所以我想知道你们中的任何人都可以帮我吗.

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.

更新

@alecxe,您的解决方案运行完美(除了将我的出生日期强制转换为日期时间格式,而不是常规字符串).

@alecxe, your solution works perfectly (except its casting my date of birth as a datetime format instead of a regular string).

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']]

由于我是一个初学者,所以我想知道如果我使用for循环而不是列表推导,这将如何工作.

Since I'm a beginner I wanted to know how this would work if I used a for loop instead of a list comprehension.

所以我用了这个:

def iter_rows(ws):
result=[]
for row in ws.iter_rows()
    for cell in row:
        result.append(cell.value)
yield result

几乎 给了我完全相同的输出,相反,它给了我这个: 如您所知,从本质上讲,它给了我一个巨大的列表,而不是您给我的结果中的嵌套列表.

It almost gives 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']]

推荐答案

iter_rows() has probably a similar sense:

使用range_string参数返回平方范围 发电机.如果未通过范围,则将遍历该对象中的所有单元格 工作表

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

>>> 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]

演示:

>>> 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]]

这篇关于查看openpyxl中的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆