I am reading data in from a text file so each row is a list of strings, and all those lists are in a data list. So my lists look like:
data = [row1, row2, etc.]
Also in your in this case you can use list comprehension
data = [[item.replace("%", "").replace("$", "0") for item in row] for row in data]
The problem is that your str variable is shadowing the builtin Python str variable. That fix is easy. Just use another variable name.
The second problem is that the replaced string isn't being replaced in the row list itself. The fix is to save the replaced string back into the row list. For that, you can use enumerate() to give you both the value and its position in the row:
for row in data:
for i, x in enumerate(row):
x = x.replace("%","")
x = x.replace("$","")
row[i] = x
You are assigning a new value to the name x
but that does not change the contents of row
or data
. After changing x
, you need to assign row[j] = x
or data[i][j] = x
for the appropriate column index j
(and row index i
). See also python 3: lists dont change their values