Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Excel file reading problem
#1
Hello to all,

the following program should indicate the average number of emissions by reading an excel file.
Excel file has to columns, one for company, the other for emissions.

Do you know why total_emissions=0?
I think the program is not able to read the excel file correctly but I don't know what else to do.

The goal would be to have 0.5*6008+0.5*4051=5029 and so D- grade.
Thanks

Code is:
from openpyxl import load_workbook
wb = load_workbook('')
sheet = wb.active
companies = input('Enter the companies in the portfolio, separated by commas: ').split(',')
weights = input('Enter the weights of each company, separated by commas: ')
total_emissions = 0
for i in range(len(companies)):
  # Get the company name and weight
  company = companies[i]
  weight = weights[i]
  for row in sheet.rows:
    if row[0].value == company:
      total_emissions += row[1].value * weight
print(total_emissions)
if total_emissions <= 250:
  grade = 'A+'
elif total_emissions <= 500:
  grade = 'A'
elif total_emissions <= 750:
  grade = 'A-'
elif total_emissions <= 1000:
  grade = 'B+'
elif total_emissions <= 1250:
  grade = 'B'
elif total_emissions <= 1500:
  grade = 'B-'
elif total_emissions <= 2000:
  grade = 'C+'
elif total_emissions <= 2500:
  grade = 'C'
elif total_emissions <= 3000:
  grade = 'C-'
elif total_emissions <= 4000:
  grade = 'D+'
elif total_emissions <= 5000:
  grade = 'D'
elif total_emissions <= 6000:
  grade = 'D-'
else:
  grade = 'F'

print(f'The grade for the portfolio is: {grade}')
Enter the companies in the portfolio, separated by commas: LAFARGE HOLCIM, TECHNIPFMC PLC
Enter the weights of each company, separated by commas: 0.5,0.5
0
The grade for the portfolio is: A+
Reply
#2
Your posted code cannot work. weight is not a number. I pulled out the code where you enter the companies and weights.
companies = input('Enter the companies in the portfolio, separated by commas: ').split(',')
weights = input('Enter the weights of each company, separated by commas: ')
for i in range(len(companies)):
    company = companies[i]
    weight = weights[i]
    print(company, weight)
Output:
A 1 B , C
Two things to note.
1. weight is a string, not a number. The weights are "1", ",", " ".
2. company strings include leading whitespace.

Consider using pandas instead of openpyxl. pandas will do a much better job reading an excel spreadsheet than any code you or I can hope to write.
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 340 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 452 Feb-07-2024, 12:24 PM
Last Post: Viento
Sad problems with reading csv file. MassiJames 3 648 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Search Excel File with a list of values huzzug 4 1,259 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 853 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  trouble reading string/module from excel as a list popular_dog 0 430 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading a file name fron a folder on my desktop Fiona 4 923 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,113 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,081 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 822 Feb-16-2023, 08:11 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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