【问题标题】:Replace specific values in openpyxl替换 openpyxl 中的特定值
【发布时间】:2017-03-03 04:17:29
【问题描述】:

我有一个如下所示的 excel 文件:

   1984    1      1
   1985    1      1

我想将第 2 列中的所有值更改为 0,但我不确定如何循环遍历行。

我试过了:

import openpyxl

wb=openpyxl.load_workbook(r'C:\file.xlsx')
ws=wb['Sheet1']
for row in ws:
     row = [x.replace('1', '0') for x in row]

但这不能是你遍历行的方式。

我想要的输出是:

1984 0 1
1985 0 1

【问题讨论】:

    标签: python-2.7 csv openxls


    【解决方案1】:

    你可以这样做:

    import openpyxl
    excelFile = openpyxl.load_workbook('file.xlsx')
    sheet1 = excelFile.get_sheet_by_name('Sheet1')
    currentRow = 1
    for eachRow in sheet1.iter_rows():
        sheet1.cell(row=currentRow, column=2).value = "0"
        currentRow += 1
    excelFile.save('file.xlsx')
    

    将第二列更新为全零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多