python 想导入excel中的数据,求各位大神看看有什么错误

叫我木木好啦 2018-11-28 02:36:18
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import numpy
>>> from openpyxl import load_workbook

##读取路径
wb = load_workbook(filename=r'D:\密闭取芯综合数据.xlsx')
##读取名字为Sheet1的sheet表
ws = wb.get_sheet_by_name("密闭取芯综合数据")
arr1 = []
arr2 = []
## 遍历第2行到10000行
for row_A in range(2, 10000):
## 遍历第2行到10000行,第1列
a1 = ws.cell(row=row_A, column=23).value
## 遍历第2行到10000行,第3列
a2 = ws.cell(row=row_A, column=24).value
if a1:
# 写入数组1
arr1.append(a1)
if a2:
# 写入数组2
arr2.append(a2)
...全文
303 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
a381518303 2018-12-24
  • 打赏
  • 举报
回复
路径,D:\\
csbinchina 2018-12-01
  • 打赏
  • 举报
回复
openpyxl版本與python不一致,一個64位的,一個32位的。
ruancan 2018-11-30
  • 打赏
  • 举报
回复
之前写了个excel的读取,分享给你

import xlrd


class ExcelToData:

    @staticmethod
    def read_to_list(file: str) -> list:
        """
        取出excel文件中的所有信息
        :param file: excel文件名(带路径)
        :return: 二维list,外层为sheet,内层为行,元素为单元格
        """
        excel = xlrd.open_workbook(file)
        result_list = []
        for sheet in excel.sheets():
            sheet_list = []
            for row in range(sheet.nrows):
                row_list = []
                for cols in range(sheet.ncols):
                    row_list.append(sheet.cell(row, cols).value)
                sheet_list.append(row_list)
            result_list.append(sheet_list)
        return result_list
szbach 2018-11-28
  • 打赏
  • 举报
回复
传入sheet的名称而非excel文件名称
luckycdy 2018-11-28
  • 打赏
  • 举报
回复
get_sheet_by_name(),这个函数的参数是你 excel 的 sheet 名称(excel 左下角的页签名称,不是excel 名称)。 另外,这个函数已经弃用了,你最好用 ws = wb['sheetName'] 来代替,不然会出现警告。
陈年椰子 2018-11-28
  • 打赏
  • 举报
回复
运行有错误? sheet 找不到?

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧