Reputation: 115
def organize_data(location='G:\pythonFiles\problem 22.txt'):
with open(location) as f:
name_string = f.read().replace('\n', '')
name_string.replace('"', '')
names = name_string.split(',')
return names
print organize_data()
It seems like the replace
method isnt working, because with or without it im getting the same result: ['"MARY"', '"PATRICIA"', '"LINDA"', '"BARBARA"',.....]
How can i remove all "
and return a list like that: ['MARY', 'PATRICIA', 'LINDA', 'BARBARA',.....]
``
Upvotes: 0
Views: 1524
Reputation: 2135
First of all, use open with encoding, name_string.replace('"', '')
means that you are working with utf-8.
Try this
def organize_data(location='G:\pythonFiles\problem 22.txt'):
with open(location, "r", encoding="utf-8") as f:
name_string = f.read()replace('\n', '')
name_string = name_string.replace('"', '')
names = name_string.split(',')
return names
print organize_data()
Upvotes: 0
Reputation: 601411
Strings in Python are immutable, so string methods can't modify a string in place. They instead return a modified version of the string.
name_string.replace('"', '')
as a separate statement does not do anything. It returns the string with double quotes removed, but the return value is not stored anywhere. So you should use
name_string = name_string.replace('"', '')
instead.
Upvotes: 4