python 为什么不能转换为浮点型[duplicate]

v8wbuo2f  于 2023-03-11  发布在  Python
关注(0)|答案(2)|浏览(205)

此问题在此处已有答案

How to skip the headers when processing a csv file using Python?(5个答案)
2小时前关门了。
我试图将我的CSV文件转换为某些数字上的浮点数,但我不断得到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)

我的CSV数据是

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,,,,,,,,

数值误差

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]'

我尝试使用分割和剥离,但它们在字符串中无效

g6ll5ycj

g6ll5ycj1#

看起来CSV数据的第一行是标题--而循环没有考虑到这一点,所以实际上是要将标题转换为浮点数。
如果这是CSV文件的唯一情况,并且在这种情况下跳过第一行就足够了,那么您可以添加:

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

在开始循环之前,您的转换应该可以工作。

zysjyyx4

zysjyyx42#

错误消息告诉您,它试图将'Access to clean fuels and technologies for cooking (% of population) [EG.CFT.ACCS.ZS]'转换为浮点型。这是第一行(可能是列名)中不包含数字的值。您应该跳过第一行。您还应该检查其他无效数据条件,因为示例中的最后几行包含空列和不一致的数据。

相关问题