openpyxl 返回数值类型
我有一个用Python写的脚本,使用openpyxl来读取Excel文件。之前这个脚本运行得很好,直到我发现openpyxl没有正确安装,这导致我在IDE外运行脚本时出现错误。不过修复了这个问题后,脚本返回了一些我搞不懂的数字,而不是我想要的真实值。
这个脚本是:
wb=load_workbook(r'C:\test.xlsx', use_iterators = True)
ws=wb.get_sheet_by_name('Sheet1')
#Iterate trough all rows
for row in ws.iter_rows(row_offset=1):
for cell in row:
#If the column == A, check if there's a website value
if cell.column == 'A':
try:
print cell.internal_value
self.match = re.match(regex, cell.internal_value)
if self.match:
self.match = 'OK'
except:
pass
在try块中的打印语句是为了查看程序返回了什么,前五条记录的返回结果如下:
0
1
31
49
143
它应该是:
None
Website
www.coolblue.nl
www.bol.com
www.elektrosky.nl
为什么我的脚本返回这些数字,而不是实际的值呢?
编辑:我的xml文件的前六行(第一行是空的)
Website | Sender | Price | Mark(s) | Payment methods
www.coolblue.nl PostNL Free Thuiswinkel Ideal, Visa, Mastercard
www.bol.com PostNL Free Thuiswinkel Ideal, Visa, Mastercard
www.elektrosky.nl PostNL € 5,00 Webshop keurmerk Ideal, Visa, Mastercard, Amex, PayPal
www.belsimpel.nl PostNL, DPD € 6,95 Thuiswinkel Ideal, Visa, Mastercard
1 个回答
2
问题在于你使用了 .internal_value
。默认情况下,Excel 会把字符串存储在一个查找表里,并在单元格中保留一个索引。如果你直接使用 .value
,应该就没问题了。