<bdo id='FxYX4'></bdo><ul id='FxYX4'></ul>

    <tfoot id='FxYX4'></tfoot>
    <i id='FxYX4'><tr id='FxYX4'><dt id='FxYX4'><q id='FxYX4'><span id='FxYX4'><b id='FxYX4'><form id='FxYX4'><ins id='FxYX4'></ins><ul id='FxYX4'></ul><sub id='FxYX4'></sub></form><legend id='FxYX4'></legend><bdo id='FxYX4'><pre id='FxYX4'><center id='FxYX4'></center></pre></bdo></b><th id='FxYX4'></th></span></q></dt></tr></i><div id='FxYX4'><tfoot id='FxYX4'></tfoot><dl id='FxYX4'><fieldset id='FxYX4'></fieldset></dl></div>
  1. <small id='FxYX4'></small><noframes id='FxYX4'>

  2. <legend id='FxYX4'><style id='FxYX4'><dir id='FxYX4'><q id='FxYX4'></q></dir></style></legend>
    1. 为什么这个循环的迭代没有在openpyxl中添加单元格?

      时间:2023-10-19
      <i id='or1rj'><tr id='or1rj'><dt id='or1rj'><q id='or1rj'><span id='or1rj'><b id='or1rj'><form id='or1rj'><ins id='or1rj'></ins><ul id='or1rj'></ul><sub id='or1rj'></sub></form><legend id='or1rj'></legend><bdo id='or1rj'><pre id='or1rj'><center id='or1rj'></center></pre></bdo></b><th id='or1rj'></th></span></q></dt></tr></i><div id='or1rj'><tfoot id='or1rj'></tfoot><dl id='or1rj'><fieldset id='or1rj'></fieldset></dl></div>
      • <legend id='or1rj'><style id='or1rj'><dir id='or1rj'><q id='or1rj'></q></dir></style></legend>
          <tbody id='or1rj'></tbody>
        • <bdo id='or1rj'></bdo><ul id='or1rj'></ul>
          <tfoot id='or1rj'></tfoot>

              <small id='or1rj'></small><noframes id='or1rj'>

                本文介绍了为什么这个循环的迭代没有在openpyxl中添加单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                将以下内容作为 xlsx roi.xlsx 的第一页的内容:

                Given the following as the contents of the first sheet of an xlsx roi.xlsx:

                然后:

                wb = load_workbook('roi.xlsx', data_only=True)
                ws=wb.worksheets[0]
                keynames = [i.value for i in ws.columns[0]]
                

                我想将 values 从以下字典添加到 B 列:

                I want to add values into column B from the following dict:

                mydict = {'carnival': 2, 'festival': 3}
                

                当我尝试时:

                for k, v in mydict.items():
                    keyPosition = keynames.index(k)
                    ws.cell(row = keyPosition, column = 2).value = v
                

                我最终得到一个新列 B,但整个值为空,AND 当我在使用 wb.save 保存文件后重新打开文件时,Excel 无法打开文件,因为它已损坏.

                I end up with a new column B, but empty values throughout, AND when I go to re-open the file after saving it with wb.save, Excel can't open the file because it is corrupted.

                推荐答案

                我认为问题是您使用 keynames 的索引存储在 B 列中,索引从 0 开始,但是B 列没有任何 0 行.理想情况下,您应该得到 - openpyxl.utils.exceptions.CellCoordinatesException: There is no row 0 (B0)

                I think the issue is that you are using the index of keynames to store in the B column, index starts at 0 , but column B does not have any 0 row. You should ideally be getting - openpyxl.utils.exceptions.CellCoordinatesException: There is no row 0 (B0)

                改用此代码(开始更改第 1 行的值,而不是 0)-

                Try this code instead (to start changing values at row 1,instead of 0) -

                for k, v in mydict.items():
                    keyPosition = keynames.index(k)
                    ws.cell(row = keyPosition + 1, column = 2).value = v
                

                这篇关于为什么这个循环的迭代没有在openpyxl中添加单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 python 的元组列表中有效且更快地迭代超过 3600 万个项目 下一篇:将迭代转化为递归

                相关文章

                1. <legend id='gmFCo'><style id='gmFCo'><dir id='gmFCo'><q id='gmFCo'></q></dir></style></legend>
                  • <bdo id='gmFCo'></bdo><ul id='gmFCo'></ul>
                  <tfoot id='gmFCo'></tfoot>

                  <small id='gmFCo'></small><noframes id='gmFCo'>

                    <i id='gmFCo'><tr id='gmFCo'><dt id='gmFCo'><q id='gmFCo'><span id='gmFCo'><b id='gmFCo'><form id='gmFCo'><ins id='gmFCo'></ins><ul id='gmFCo'></ul><sub id='gmFCo'></sub></form><legend id='gmFCo'></legend><bdo id='gmFCo'><pre id='gmFCo'><center id='gmFCo'></center></pre></bdo></b><th id='gmFCo'></th></span></q></dt></tr></i><div id='gmFCo'><tfoot id='gmFCo'></tfoot><dl id='gmFCo'><fieldset id='gmFCo'></fieldset></dl></div>