使用 openpyxl 将 Excel 电子表格转换为 CSV,如何将零变成空白?

Using openpyxl to convert Excel spreadsheets to CSVs, how can I turn zeros into blanks?

我有一个简单的代码,用于从工作簿中读取特定的价差sheet,并将其值写入 csv。 spreadsheet 的公式非常繁重,因为它从 raw_data sheet 中获取数据并对其进行处理。但是,有些单元格没有添加任何信息,因此单元格为空白。但是由于那里有一个公式,openpyxl 将其变为零。如何将这些零转换为 2 个逗号之间的空 space?

import openpyxl
import csv

wb = openpyxl.load_workbook('C:\Users\Staff\Desktop\Test\MS098_AlexLibRecords.xlsx', data_only = True)
sh = wb["MODS"]
with open('C:\Users\Staff\Desktop\Test\MODS_OR.csv', 'wb') as f:
    c = csv.writer(f)
    for r in sh.rows:
        c.writerow([cell.value for cell in r])

你试过这个解决方案了吗?

for r in sh.rows:
    out_val_list = list()
    for cell in r:
        out_val = cell.value
        if out_val == 0 or str(out_val).strip() == "0":
            out_val = None
        out_val_list.append(out_val)
    c.writerow(out_val_list)