在python的csv模块中,有一个名为csv.reader的函数,它允许您遍历一行,返回一个reader对象,并且可以保存在像列表这样的容器中.
因此,当列表分配给变量并打印时,即:
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中看到类似的功能.我可能会弄错,所以我想知道你们中是否有人可以帮助我.
更新
@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']]
由于我是初学者,我想知道如果我使用for循环而不是列表理解,这将如何工作.
所以我用过这个:
def iter_rows(ws):
result=[]
for row in ws.iter_rows()
for cell in row:
result.append(cell.value)
yield result
它几乎给了我完全相同的输出,相反它给了我这个:
正如你所知,它基本上给了我一个巨大的列表而不是你给我的结果中的嵌套列表.
>>>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()
可能有类似的意义:
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>)]
您可以稍微修改它以生成行值列表,例如:
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]]