LIMITED TIME OFFER: 50% OFF OF PREMIUM WITH OUR ANNUAL PLAN (THAT'S $294 IN SAVINGS).
GET OFFER

Stuck on Step 1, Course 2, Lesson 6

Screen Link:

My Code:

from csv import reader
opened_file = open('artworks.csv')
read_file = reader(opened_file)
moma = list(read_file)
moma = moma[1:]

for row in moma:
    nationality = row[2]
    nationality = nationality.replace("(","")
    nationality = nationality.replace(")","")
    row[2] = nationality

    gender = row[5]
    gender = gender.replace("(","")
    gender = gender.replace(")","")
    row[5] = gender

for row in moma:
    gender = row[5]
    gender = gender.title()
    if not gender:
        gender = "Gender Unknown/Other"
    row[5] = gender
nationality = row[2]
    nationality = nationality.title()
    if not nationality:
        nationality = "Nationality Unknown"
    row[2] = nationality
##INITIAL CODE ENDS

def clean_and_convert(date):
    if date != "":
        date = str(date.replace("(", ""))
        date = str(date.replace(")", ""))
        date = int(date)
    return date
        
for row in moma:
    BeginDate = moma[3]
    EndDate = moma[4]
    clean_and_convert(BeginDate)
    clean_and_convert(EndDate)
    moma[3] = BeginDate
    moma[4] = EndDate

What I expected to happen:
I expected the brackets around the dates to be removed, the dates to be converted to ints, and have this cleaned data returned to the dataset.

What actually happened:
I get an error message, and I don’t know how to fix it. I would like to know where I am going wrong.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-701fc9f01379> in <module>
     73     BeginDate = moma[3]
     74     EndDate = moma[4]
---> 75     clean_and_convert(BeginDate)
     76     clean_and_convert(EndDate)
     77     moma[3] = BeginDate

<ipython-input-1-701fc9f01379> in clean_and_convert(date)
     64 def clean_and_convert(date):
     65     if date != "":
---> 66         date = str(date.replace("(", ""))
     67         date = str(date.replace(")", ""))
     68         date = int(date)

AttributeError: 'list' object has no attribute 'replace'

Thank you!

[I am working below with the assumption that you are relatively new to programming in general. So, it’s an attempt to help you understand how to dissect and resolve such errors]

When you face any error in python, there is sort of a blueprint you can rely on to resolve some of them -

  1. Look at the last part of the error message directly. It says AttributeError: 'list' object has no attribute 'replace' which tells you that you get an error because a list object has no attribute replace. Which means that you are trying to use replace in your code somewhere on a list.
  2. Based on the above, look at where the error originates. This is pointed to us in the message through those arrows we can see. We can see that one of the arrow points to date = str(date.replace("(", ""))
  3. From above two we can notice that there is a replace() function used there in that code line the arrow points to. So, why is it an issue?

At this stage, once you get used to understanding error messages, you will be able to make the connection that date is a list in your code and that’s why you get the error. Because a list object in Python doesn’t have a replace() method. Which leads to two questions that you can (and should) ask yourself -

  • Ok, so if list doesn’t have a replace() method then what does?
  • And if that throws an error, then should date be a list?

The problem to focus upon is that date is a list. We want it to be a string instead as per the problem definition, and because strings in Python do have a replace() method. So, that’s what you need to resolve.

If you struggle to make the connection above, don’t worry, that’s something you will get better at as you code more. But, there is also a fourth important step to the blueprint I mentioned above -

  1. Google that error. Simply copying and pasting that error into google will show you multiple results to help you figure it out and make the connection I shared above. Sometimes, you might have to go through multiple results to figure out which one can help you. But more often than not, after spending some time, you will find a solution as you are learning.

Now that you have narrowed the problem down, try to think of why your date is a list and not a string in your code. See where you call that function, what variable(s) you pass to it, what value(s) that variable(s) stores. If you get stuck, feel free to ask more questions.

1 Like

Thank you! I am very much a beginner and appreciate the comprehensive explanation.

2 Likes

Okay, this is getting embarrassing. I can tell it is something incredibly obvious and basic.

I googled the error message and while there were several answers, I did not find one that seemed applicable.

What I can tell:
The problem is I am calling the replace function on a list. So I tried converting it to a string with the str() method but still receive the same error message.

I tried isolating the specific item in the list and then converting it to a string, and it is still giving me an error message.

I know this answer is staring me in the face, and it is probably something “stupid-simple” but I just don’t see it. Help!

Nothing to be embarrassed about. As long as you are putting an effort, it’s all good.

So, date being a list instead of a string means that somewhere in your code you are assigning date as a list or storing it as a list.

That’s when you need to backtrack through your code and see where that might be happening. date is just an argument for the function clean_and_convert().

Above, I mentioned looking at the arrows in your error message. One of the arrows points to

clean_and_convert(BeginDate)

That’s where the error originated. So, date is actually BeginDate. That means BeginDate is a list when it should be a string.

Now, you define BeginDate as -

BeginDate = moma[3]

So, what is moma and what is moma[3]. You print these out and you will get a list and not a string.

But why is that?

If you are looping through the dataset why would you get a list and not a string? Surely, as you iterate through each data point you should get a single value instead of a list, right?

And that’s the source of the issue, your for loop states -

for row in moma:

Each iteration looks at a row from moma. Each row is a list and from each row you extract the date which is a string.

However, inside your for loop you don’t use row. You directly use moma which is a list of lists.

So, instead of using row[3] which would mean the 3rd element in the list row, you use moma[3] which means the third list in the list of lists moma.

That’s what you need to fix.

1 Like

I am still wrapping my head around how Python and how it’s logic works and fits together. This was an incredibly helpful explanation of what is going on and why. Thank you so much!

1 Like