替换 openpyxl 中的特定值

Ste*_*ter 1 csv openxls python-2.7

我有一个看起来像这样的excel文件:

   1984    1      1
   1985    1      1
Run Code Online (Sandbox Code Playgroud)

我想将第 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]
Run Code Online (Sandbox Code Playgroud)

但这一定不是您遍历行的方式。

我想要的输出是:

1984 0 1
1985 0 1
Run Code Online (Sandbox Code Playgroud)

Aug*_*e C 5

你可以这样做:

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')
Run Code Online (Sandbox Code Playgroud)

将第二列更新为全零。