![]() |
Project, Reading Data from a spreadsheet. Error message!! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Project, Reading Data from a spreadsheet. Error message!! (/thread-21346.html) |
Project, Reading Data from a spreadsheet. Error message!! - Shafla - Sep-25-2019 i have written the following code from an exercise in a text book; import openpyxl, pprint print('Opening workbook...') wb = openpyxl.load_workbook('censuspopdata.xlsx') sheet=wb.get_sheet_by_name('Population by Census Tract') countyData={} print ('Reading Rows') for row in range(2, sheet.max_row + 1): state = sheet['B' + str(row)].value county = sheet['C' + str(row)].value pop = sheet['D' + str(row)].value for row in range(2,sheet.max_row+1): state = sheet['B' + str(row)].value county = sheet['C' + str(row)].value pop = sheet['D' + str(row)].value countyData.setdefault(state, {}) countyData[state].setdefault(county, {'tracts':0,'pop':0}) countyData[state][county]['tracts']+=1 countyData[state][county]['pop']+=int(pop) for row in range(2, sheet.max_row+1): print('writing results...') resultFile=open('census2010.py', 'w') resultFile.write('allData =' + pprint.pformt(countyData)) resultFile.close() print ('Done') once i run the code i get the following error messages; i have not come across Deprecation Warning before. and i do not understand the second error. could anybody tell me how to correct my code?ive realised my spelling mistake in pformt.. when ive corrected it and i still get the first error message Warning (from warnings module): File "/Users/shaneshomefolder/Desktop/readCenExel.py", line 4 sheet=wb.get_sheet_by_name('Population by Census Tract') DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]). and then a constant loop of writing results... Done writing results... Done writing results... Done writing results... Done writing results... Done writing results... Done writing results... Done writing results... RE: Project, Reading Data from a spreadsheet. Error message!! - buran - Sep-27-2019 Depreciation warning is clear instead of sheet=wb.get_sheet_by_name('Population by Census Tract') use sheet=wb['Population by Census Tract'] Looking at the file name it's a census data, so I guess great number of rows/records. You print writing results... and Done after writing each row, so I would expect some long output.Note that it doesn't make to open and close the file for every row. Also it's better to use with context manager when open the file and thus no need to close it.Once again - I would advise you to find more up to date/better book to follow |