Orsons
Orsons

Reputation: 51

Replace characters in string by iterating over the characters

x = ['"yes',
 '3',
 '""McGowan Miss. Anna """"Annie""""""',
 'female#',
 '15',
 '8.0292"']

for string in x:
    for character in string:
        character = character.replace('"','')
x

gives

['"yes',
 '3',
 '""McGowan Miss. Anna """"Annie""""""',
 'female#',
 '15',
 '8.0292"']

and I don't know why its not removing the " from the strings...

Thanks in advance!

EDIT:

how would I iterate through a List of List?

x = [['"yes',
 '3',
 '""McGowan Miss. Anna """"Annie""""""',
 'female#',
 '15',
 '8.0292"'],[True,
  1,
  'Futrelle Mrs. Jacques Heath (Lily May Peel)',
  'female',
  '35',
  '53.1']]

with

for i in x:
    for b in i:
        b[i] = b[i].replace('"', '')

doesnt work...

EDIT2: fixed, my x had integers and booleans

Upvotes: 0

Views: 114

Answers (2)

Derek Wang
Derek Wang

Reputation: 10194

You need to replace the string, not character.

x = ['"yes',
 '3',
 '""McGowan Miss. Anna """"Annie""""""',
 'female#',
 '15',
 '8.0292"']

for i in range(len(x)):
    x[i] = x[i].replace('"', '')

print(x)

Upvotes: 1

wind
wind

Reputation: 2441

Replace you whole for loop with this:

for i, string in enumerate(x):
    x[i] = string.replace('"','')

Upvotes: 3

Related Questions