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
  docx file to pandas dataframe/excel iitip92 1 2,599 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  Reading an ASCII text file and parsing data... oradba4u 2 1,437 Jun-08-2024, 12:41 AM
Last Post: oradba4u
  Python openyxl not updating Excel file MrBean12 1 2,049 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 1,585 Feb-07-2024, 12:24 PM
Last Post: Viento
Sad problems with reading csv file. MassiJames 3 2,576 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Search Excel File with a list of values huzzug 4 2,830 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 2,074 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  trouble reading string/module from excel as a list popular_dog 0 928 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading a file name fron a folder on my desktop Fiona 4 2,107 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 2,106 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone

Forum Jump:

User Panel Messages

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