My Code:
test_data = ["1912", "1929", "1913-1923",
"(1951)", "1994", "1934",
"c. 1915", "1995", "c. 1912",
"(1988)", "2002", "1957-1959",
"c. 1955.", "c. 1970's",
"C. 1990-1999"]
bad_chars = ["(",")","c","C",".","s","'", " "]
def strip_characters(string):
for char in bad_chars:
string = string.replace(char,"")
return string
stripped_test_data = ['1912', '1929', '1913-1923',
'1951', '1994', '1934',
'1915', '1995', '1912',
'1988', '2002', '1957-1959',
'1955', '1970', '1990-1999']
def process_date(string):
if "-" in string:
dates = string.split("-")
for year in dates:
year = int(year)
#dates = [int(year) for year in dates]
update = round(sum(dates)/2)
else:
update = int(string)
return update
processed_test_data = []
for date in stripped_test_data:
update = process_date(date)
processed_test_data.append(update)
for row in moma:
date = row[6]
date = strip_characters(date)
date = process_date(date)
row[6] = date
What I expected to happen:
I expected the
for year in dates:
year = int(year)
portion of my code to work as how the commented out line below it does:
dates = [int(year) for year in dates]
What actually happened:
To my understanding, it seems that the for loop is not converting all elements to int.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-17c6921909ff> in <module>
88 processed_test_data = []
89 for date in stripped_test_data:
---> 90 update = process_date(date)
91 processed_test_data.append(update)
92
<ipython-input-1-17c6921909ff> in process_date(string)
81 #dates = [int(year) for year in dates]
82
---> 83 update = round(sum(dates)/2)
84 else:
85 update = int(string)
TypeError: unsupported operand type(s) for +: 'int' and 'str'