繁体   English   中英

带有 xlrd nrows 的.xlsx 文件中的最后一行

[英]Last row in .xlsx file with xlrd nrows

我正在尝试从 a.xlsx 文件和 a.json 文件中交叉信息。 但是,我在到达.xlsx 文件的最后一行时遇到了问题...如果 j ==仅在j == 4823时,后面的代码块才有效,这很奇怪,因为理论上文件的结尾,是 10418... 不知何故,代码没有读取结束行的值,它应该。 您知道如何在不使用条件常量的情况下解决此问题吗?

.xlsx 文件的结尾如下所示: 在此处输入图像描述

Python代码:

# DEFINE INPUT JSON FILE
at_file = open(glob('*.json')[0])
data = json.load(at_file)
# DEFINE INPUT XLSX FILE
s_file = xlrd.open_workbook(glob('BAU*.xlsx')[0])
s_file_sheet = s_file.sheet_by_index(0)
# GO THROUGH JSON FILE
print(s_file_sheet.nrows)  -------------------------------------------------->  output: 140418
for i in range(0, len(data['results'])):
    if 'windows' in data['results'][i]['variables']:
        # GO THROUGH XLSX FILE
        for j in range(s_file_sheet.nrows):
            if (s_file_sheet.cell_value(j, 78) == 'BANCO' and
                    s_file_sheet.cell_value(j, 113) == 'MICROSOFT'):
                if ((data['results'][i]['name']).replace(
                        '.banco', '').lower() ==
                        s_file_sheet.cell_value(j, 91).lower()):
                    break
                if j == s_file_sheet.nrows - 1:  
                    print((data['results'][i]['name']).replace( ------------->  output: None
                         '.banco', '').lower())

You can Use Pandas read_xlsx or it can Be More Easier if you can Convert the Excel file into CSV and use pandas read_csv and do you Operations and then write it to JSON.. Pandas is till the Best Package for Doing operations on Excel / CSV / TSV 文件.. 你甚至可以使用 Pythons CSV 模块来做同样的事情..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM