[英]Last row in .xlsx file with xlrd nrows
我正在嘗試從 a.xlsx 文件和 a.json 文件中交叉信息。 但是,我在到達.xlsx 文件的最后一行時遇到了問題...如果 j ==僅在j == 4823時,后面的代碼塊才有效,這很奇怪,因為理論上文件的結尾,是 10418... 不知何故,代碼沒有讀取結束行的值,它應該。 您知道如何在不使用條件常量的情況下解決此問題嗎?
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.