Python Forum
Project, Reading Data from a spreadsheet. Error message!!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Project, Reading Data from a spreadsheet. Error message!!
#1
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;
Error:
Opening workbook... 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]). Reading Rows writing results... Traceback (most recent call last): File "/Users/shaneshomefolder/Desktop/readCenExel.py", line 31, in <module> resultFile.write('allData =' + pprint.pformt(countyData)) AttributeError: module 'pprint' has no attribute 'pformt' >>>
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...
Reply


Messages In This Thread
Project, Reading Data from a spreadsheet. Error message!! - by Shafla - Sep-25-2019, 08:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 276 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,411 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Returning Column and Row Data From Spreadsheet knight2000 0 540 Oct-22-2023, 07:07 AM
Last Post: knight2000
  Editing spreadsheet/csv BSDevo 6 1,236 Sep-01-2023, 05:47 PM
Last Post: BSDevo
  Error message about iid from RandomizedSearchCV Visiting 2 1,200 Aug-17-2023, 07:53 PM
Last Post: Visiting
  xlwings error when reading a workbook Mishal0488 1 1,287 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  Another Error message. the_jl_zone 2 1,090 Mar-06-2023, 10:23 PM
Last Post: the_jl_zone
  Looking to automate updating a spreadsheet with image from email cubangt 2 1,107 Feb-14-2023, 03:43 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 963 Jan-24-2023, 02:48 PM
Last Post: demdej
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,546 Nov-30-2022, 10:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020