-4

I am trying to convert my CSV file into floats on certain ones that are numbers, However I keep getting ValueErrors.

def create_info(self, country_data):
    with open(country_data, "r") as csvfile:
      csvreader = csv.reader(csvfile)
      for words in csvreader:
    
        

        c = Country()

        c.country_name = words[2]
        c.country_code = words[3]
        c.acc_to_clean_fuel = float(words[4])
        c.acc_to_elec = float(words[5])
        c.annual_freshwater_withdrawl = float(words[6])
        c.armed_forces_tot = int(words[7])
        c.expense_gdp = float(words[8])

        self.__list_countries.append(c)
      

my CSV data is

Time,Time Code,Country Name,Country Code,Access to clean fuels and technologies for cooking (% of population) [EG.CFT.ACCS.ZS],Access to electricity (% of population) [EG.ELC.ACCS.ZS],"Annual freshwater withdrawals, total (% of internal resources) [ER.H2O.FWTL.ZS]","Armed forces personnel, total [MS.MIL.TOTL.P1]",Expense (% of GDP) [GC.XPN.TOTL.GD.ZS]
2018,YR2018,Argentina,ARG,99.8,99.9895782470703,12.9075342465753,105200,22.6877806247463
2018,YR2018,Australia,AUS,100,100,2.5254882398374,58100,26.2209281816529
2018,YR2018,"Bahamas, The",BHS,100,100,..,1300,17.4708838332366
2018,YR2018,Brazil,BRA,95.8,99.6999969482422,1.14131778837661,761500,35.453999893748
2018,YR2018,China,CHN,75.6,100,21.0387855949376,2695000,..
2018,YR2018,Cuba,CUB,93.5,99.5212097167969,18.2528856243442,76000,..
2018,YR2018,France,FRA,100,100,13.425,304800,46.7732066858883
2018,YR2018,Germany,DEU,100,100,22.8439252336449,181400,27.9229226403601
2018,YR2018,Greece,GRC,100,100,17.451724137931,147850,47.9372432955748
2018,YR2018,Ireland,IRL,100,100,2.91224489795918,8650,23.5797004719187
2018,YR2018,Italy,ITA,100,100,18.6551780821918,341500,42.5466717910759
2018,YR2018,Japan,JPN,100,100,18.3953488372093,261150,16.0811786865142
2018,YR2018,Malaysia,MYS,96.1,99.9922409057617,1.15637931034483,136000,15.9220539203293
2018,YR2018,Maldives,MDV,98.8,100,15.6666666666667,..,..
2018,YR2018,Mexico,MEX,84.8,99.5,21.721271393643,348000,20.4206535576881
2018,YR2018,Poland,POL,100,100,17.9067164179104,196700,33.3764400382599
2018,YR2018,Romania,ROU,87.1,100,15.1392166116092,126600,32.7748585957104
2018,YR2018,Saudi Arabia,SAU,100,100,974.166666666667,252000,29.0920814788993
2018,YR2018,United States,USA,100,100,15.7699117024308,1379800,22.3205732607659
2018,YR2018,United Kingdom,GBR,100,100,5.80620689655172,148450,37.4283979401227
,,,,,,,,
,,,,,,,,
,,,,,,,,
Data from database: World Development Indicators,,,,,,,,
Last Updated: 03/01/2023,,,,,,,,

value Error

File "main.py", line 7, in main
    Info.create_info("country_data.csv")
  File "/home/runner/Country-step-4/CountryIRSystem.py", line 26, in create_info
    c.acc_to_clean_fuel = float(words[4])
ValueError: could not convert string to float: 'Access to clean fuels and technologies for cooking (% of population) [EG.CFT.ACCS.ZS]'

I tried to use split and strip but they were invalid in the strings

2 Answers2

0

It seems like the first row of your CSV data are your headers -- and your loop is not taking that into consideration. So you're actually trying to convert your titles to floats.

If it's your only case for a CSV file and it's enough to skip the first line just in this instance, you can just add:

  csvreader = csv.reader(csvfile)
  csvreader.__next__() # This will skip the titles

Before starting your loop and your conversions should work.

ae_bu
  • 92
  • 3
  • Thank you for the help! I was reading the value error wrong. I thought it was telling me the values in that column couldn’t be read. I just disregarded that there was a header – Elliott2232 Mar 10 '23 at 23:44
0

The error message tells you that it was trying to convert 'Access to clean fuels and technologies for cooking (% of population) [EG.CFT.ACCS.ZS]' to float. This is a value from the first line (presumably column names) which doesn't contain a number. You should skip the first line. You should also check for other invalid data conditions because, the last few lines in your sample contain empty columns and inconsistent data.

Alain T.
  • 40,517
  • 4
  • 31
  • 51