在openpyx中获取0/None作为单元格值而不是实际值

2024-09-27 17:55:17 发布

您现在位置:Python中文网/ 问答频道 /正文

得到120个值,只有5个值错误

这是目录中文件的代码段:

for entry in os.listdir(basepath):
    if os.path.isfile(os.path.join(basepath, entry)):
        if entry[len(entry)-5:] == '.xlsx' and len(entry) == 17:
            file_list.append(entry)

以下是获取数据的代码段:

 data = []
     for file in file_list:
         wb = load_workbook(self.basepath + '/' + file)
         tempData = []
         for cell in wb['Sheet1']:
             for value in cell:
                 if not value.value:
                     continue
                 else:
                     try:
                         int(value.value) + 5
                         float(value.value) + 2.4
                         tempData.append(value.value)
                     except ValueError:
                         continue
            data.append(tempData)
            tempData = []
return data

这是用于输入数据的片段的一部分:

for i in data:
    if not i:
        continue
    for j in i[::-1]:    
        if type(j) == int or type(j) == float:                
            ws[col + str(row)].value = ('%.2f' %j)

问题在于获取数据,因为使用解释器时,我发现它以某种方式获取了zerosenter图像描述 This is an example of a fileThe red coloring shows the Nones got from the example file

非常感谢您对上述代码的帮助或改进


Tags: pathinfordataifvalueos代码段

热门问题