python 读取行数据_openpyxl读取所有行数据之rows属性

2023-05-16

openpyxl读取一个sheet的所有行数据可以用rows属性,官方文档如下:

If you need to iterate through all the rows or columns of a file, you can instead use the Worksheet.rows property:

sheet的rows属性返回的是一个生成器,如下:

# -*- coding: utf-8 -*-

from openpyxl import load_workbook

wb = load_workbook('test.xlsx',data_only=True)

for sheet in wb:

sheet_name = sheet.title

data_all = sheet.rows

print('sheet名:',sheet_name)

print(data_all)

sheet名: 北京

sheet名: 上海

sheet名: 杭州

可以直接遍历生成器也可以使用tuple将生成器变为元组来输出行:

# -*- coding: utf-8 -*-

from openpyxl import load_workbook

wb = load_workbook('test.xlsx',data_only=True)

for sheet in wb:

sheet_name = sheet.title

data_all = sheet.rows

print('sheet名:',sheet_name)

data_tuple = tuple(data_all)

for row in data_tuple:

print(row)

sheet名: 北京

(, )

(, )

(, )

(, )

(, )

(, )

(, )

sheet名: 上海

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

sheet名: 杭州

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

(, )

输出元格的数据,需要value属性:

# -*- coding: utf-8 -*-

from openpyxl import load_workbook

wb = load_workbook('test.xlsx',data_only=True)

for sheet in wb:

sheet_name = sheet.title

data_all = sheet.rows

print('sheet名:',sheet_name)

data_tuple = tuple(data_all)

for row in data_tuple:

print('===一行数据如下===:')

for cell in row:

value = cell.value

print(value)

sheet名: 北京

===一行数据如下===:

贡院6号

https://bj.5i5j.com/xiaoqu/117460.html

===一行数据如下===:

万年花城二期

https://bj.5i5j.com/xiaoqu/369118.html

===一行数据如下===:

铁医新楼

https://bj.5i5j.com/xiaoqu/428075.html

===一行数据如下===:

百万庄大街乙21号楼

https://bj.5i5j.com/xiaoqu/14042.html

===一行数据如下===:

观澳园

https://bj.5i5j.com/xiaoqu/3464.html

===一行数据如下===:

天鹅湾南区

https://bj.5i5j.com/xiaoqu/10522.html

===一行数据如下===:

报国寺1号院

https://bj.5i5j.com/xiaoqu/6250.html

sheet名: 上海

===一行数据如下===:

静安豪景苑

https://sh.5i5j.com/xiaoqu/79364.html

===一行数据如下===:

新泾苑

https://sh.5i5j.com/xiaoqu/138741.html

===一行数据如下===:

兆嘉苑

https://sh.5i5j.com/xiaoqu/367631.html

===一行数据如下===:

天山怡景苑

https://sh.5i5j.com/xiaoqu/385850.html

===一行数据如下===:

金星绿苑

https://sh.5i5j.com/xiaoqu/390212.html

===一行数据如下===:

申晖小区

https://sh.5i5j.com/xiaoqu/436536.html

===一行数据如下===:

共康四村

https://sh.5i5j.com/xiaoqu/197400.html

===一行数据如下===:

崧泽华城青湖苑

https://sh.5i5j.com/xiaoqu/377290.html

===一行数据如下===:

新昌里公寓

https://sh.5i5j.com/xiaoqu/47394.html

===一行数据如下===:

建国西路158弄

https://sh.5i5j.com/xiaoqu/125420.html

===一行数据如下===:

蔷薇七村

https://sh.5i5j.com/xiaoqu/388767.html

sheet名: 杭州

===一行数据如下===:

复兴城市家园

https://hz.5i5j.com/xiaoqu/100000000001038.html

===一行数据如下===:

木材新村

https://hz.5i5j.com/xiaoqu/100000000004309.html

===一行数据如下===:

万寿亭街

https://hz.5i5j.com/xiaoqu/100000000002688.html

===一行数据如下===:

瓶窑镇凤溪路

https://hz.5i5j.com/xiaoqu/100000000000367.html

===一行数据如下===:

孝子坊

https://hz.5i5j.com/xiaoqu/100000000002902.html

===一行数据如下===:

永和坊

https://hz.5i5j.com/xiaoqu/100000000002147.html

===一行数据如下===:

教工路131号

https://hz.5i5j.com/xiaoqu/100000000001423.html

===一行数据如下===:

星星港湾琴海居

https://hz.5i5j.com/xiaoqu/100000000005718.html

===一行数据如下===:

文二路98号

https://hz.5i5j.com/xiaoqu/100000000001269.html

===一行数据如下===:

龙悦湾

https://hz.5i5j.com/xiaoqu/100000000006161.html

===一行数据如下===:

意盛花苑

https://hz.5i5j.com/xiaoqu/100000000002797.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

python 读取行数据_openpyxl读取所有行数据之rows属性 的相关文章

  • 保存为 HDF5 的图像未着色

    我目前正在开发一个将文本文件和 jpg 图像转换为 HDF5 格式的程序 用HDFView 3 0打开 似乎图像仅以灰度保存 hdf h5py File Sample h5 img Image open Image jpg data np
  • 获取GridView中选定行的索引

    我想使用复选框获取从 gridview 选择的行 复选框是这样的
  • Python 中的舍入浮点问题

    我遇到了 np round np around 的问题 它没有正确舍入 我无法包含代码 因为当我手动设置值 而不是使用我的数据 时 返回有效 但这是输出 In 177 a Out 177 0 0099999998 In 178 np rou
  • Python getstatusoutput 替换不返回完整输出

    我发现了这个很棒的替代品getstatusoutput Python 2 中的函数在 Unix 和 Windows 上同样有效 不过我觉得这个方法有问题output被构建 它只返回输出的最后一行 但我不明白为什么 任何帮助都是极好的 def
  • 用枢轴点拟合曲线 Python

    我有下面的图 我想用 2 条线来拟合它 使用 python 我设法适应上半部分 def func x a b x np array x return a x b popt pcov curve fit func up x up y 我想用另
  • Pandas 日期时间格式

    是否可以用零后缀表示 pd to datetime 似乎零被删除了 print pd to datetime 2000 07 26 14 21 00 00000 format Y m d H M S f 结果是 2000 07 26 14
  • 在Python中连接反斜杠

    我是 python 新手 所以如果这听起来很简单 请原谅我 我想加入一些变量来生成一条路径 像这样 AAAABBBBCCCC 2 2014 04 2014 04 01 csv Id TypeOfMachine year month year
  • 使用 xlrd 打开 BytesIO (xlsx)

    我正在使用 Django 需要读取上传的 xlsx 文件的工作表和单元格 使用 xlrd 应该可以 但因为文件必须保留在内存中并且可能不会保存到我不知道如何继续的位置 本例中的起点是一个带有上传输入和提交按钮的网页 提交后 文件被捕获req
  • 如何在 Python 中解析和比较 ISO 8601 持续时间? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个 Python v2 库 它允许我解析和比较 ISO 8601 持续时间may处于不同单
  • 从Python中的字典列表中查找特定值

    我的字典列表中有以下数据 data I versicolor 0 Sepal Length 7 9 I setosa 0 I virginica 1 I versicolor 0 I setosa 1 I virginica 0 Sepal
  • 如何使用python在一个文件中写入多行

    如果我知道要写多少行 我就知道如何将多行写入一个文件 但是 当我想写多行时 问题就出现了 但是 我不知道它们会是多少 我正在开发一个应用程序 它从网站上抓取并将结果的链接存储在文本文件中 但是 我们不知道它会回复多少行 我的代码现在如下 r
  • pyspark 将 twitter json 流式传输到 DF

    我正在从事集成工作spark streaming with twitter using pythonAPI 我看到的大多数示例或代码片段和博客是他们从Twitter JSON文件进行最终处理 但根据我的用例 我需要所有字段twitter J
  • Cython 和类的构造函数

    我对 Cython 使用默认构造函数有疑问 我的 C 类 Node 如下 Node h class Node public Node std cerr lt lt calling no arg constructor lt lt std e
  • 如何使用原始 SQL 查询实现搜索功能

    我正在创建一个由 CS50 的网络系列指导的应用程序 这要求我仅使用原始 SQL 查询而不是 ORM 我正在尝试创建一个搜索功能 用户可以在其中查找存储在数据库中的书籍列表 我希望他们能够查询 书籍 表中的 ISBN 标题 作者列 目前 它
  • Python:XML 内所有标签名称中的字符串替换(将连字符替换为下划线)

    我有一个格式不太好的 XML 标签名称内有连字符 我想用下划线替换它 以便能够与 lxml objectify 一起使用 我想替换所有标签名称 包括嵌套的子标签 示例 XML
  • 如何解决 PDFBox 没有 unicode 映射错误?

    我有一个现有的 PDF 文件 我想使用 python 脚本将其转换为 Excel 文件 目前正在使用PDFBox 但是存在多个类似以下错误 org apache pdfbox pdmodel font PDType0Font toUnico
  • 在本地网络上运行 Bokeh 服务器

    我有一个简单的 Bokeh 应用程序 名为app py如下 contents of app py from bokeh client import push session from bokeh embed import server do
  • Django-tables2 列总计

    我正在尝试使用此总结列中的所有值文档 https github com bradleyayers django tables2 blob master docs pages column headers and footers rst 但页
  • 如何应用一个函数 n 次? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 假设我有一个函数 它接受一个参数并返回相同类型的结果 def increment x return x 1 如何制作高阶函数repeat可以
  • 使用 z = f(x, y) 形式的 B 样条方法来拟合 z = f(x)

    作为一个潜在的解决方案这个问题 https stackoverflow com questions 76476327 how to avoid creating many binary switching variables in gekk

随机推荐