AttributeError: 'list' object has no attribute 'strip

Advertisements

I cant figure out why this isnt working and just throwing me an error. I tried to move some things areounf and its still not working. It has to be something about it not reading to the list or something like that.

from Country import Country
import csv


class CountryIRSystem:
  def __init__(self):
    self.__list_countries = []

  def search_name (self, country_name):
    for country in self.__list_countries:
      if country.country_name == country_name:
        return country_name
    return None

  def create_info(self, country_data):

    with open(country_data, "r") as csvfile:
      csvreader = csv.reader(csvfile)
      for line in csvreader:
        line = line.strip("\n")
        words = line.split()

        country = Country()

        country.country_name = words[2]
        country.country_code = words[3]
        country.acc_to_clean_fuel = words[4]
        country.acc_to_elec = words[5]
        country.annual_freshwater_withdrawl = words[6]
        country.armed_forces_tot = words[7]
        country.expense_gdp = words[8]

        self.__list_countries.append(country)

>Solution :

for line in csvreader:
    line = line.strip("\n")
    words = line.split()

csvreader returns a list of field values, not a plain line of text.

So you can just do this instead:

for words in csvreader:
    ...

Leave a ReplyCancel reply