更新时间:2023-11-25 09:13:16
OpenPyXl不存储空单元格(表示没有值,字体,边框等的空单元).如果从工作表中获得一个单元格,它将动态创建一个具有None
值的新的空单元格.
OpenPyXl doesn’t store empty cells (empty means without value, font, border, and so on). If you get a cell from a worksheet, it dynamically creates a new empty cell with a None
value.
Worksheet.iter_rows()
的当前实现(v2.4.0)使用Worksheet.cell()
方法,该方法调用没有 value 的Cell()
构造函数.
The current implementation (v2.4.0) of Worksheet.iter_rows()
use Worksheet.cell()
method which calls Cell()
constructor with no value.
您需要更改代码以处理空"单元格:
You need to change your code to handle "empty" cells:
for rownum in sh.iter_rows():
values = [(u"" if cell.value is None else unicode(cell.value))
for cell in rownum]
wr.writerow([value.encode('ascii', 'ignore') for value in rownum])
注意:由于可能是Windows用户将数据导出到CSV文件,因此您可以选择更有用的编码,例如:cp1252
.
Note: since you export your data to a CSV file for, presumably Windows users, you may choose a more useful encoding like: cp1252
.