Shikhar Dwivedi
Shikhar Dwivedi

Reputation: 45

Remove " from string

with open("p022_names.txt","r") as f:
    for line in f:
        name=[str(v) for v in line.split(",")]
name.sort()
print(name)
tot=0
for i in range(len(name)):
    tot=tot+sum(ord(letter)-64 for letter in name[i])
print(tot)

While reading data from files it is also adding " and ' into the name. If the name is COLIN it is saved in the list name as '"COLIN"'. How do I fix it? Input file.

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY","CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE","CAROLYN","CHRISTINE","MARIE","JANET","CATHERINE","FRANCES","ANN","JOYCE","DIANE","ALICE","JULIE","HEATHER","TERESA","DORIS","GLORIA","EVELYN","JEAN","CHERYL","MILDRED","KATHERINE","JOAN","ASHLEY","JUDITH","ROSE","JANICE","KELLY","NICOLE","JUDY","CHRISTINA","KATHY","THERESA","BEVERLY","DENISE","TAMMY","IRENE","JANE","LORI","RACHEL","MARILYN","ANDREA","KATHRYN","LOUISE","SARA","ANNE","JACQUELINE","WANDA","BONNIE","JULIA","RUBY","LOIS","TINA","PHYLLIS","NORMA","PAULA","DIANA","ANNIE","LILLIAN","EMILY","ROBIN","PEGGY","CRYSTAL","GLADYS","RITA","DAWN","CONNIE","FLORENCE","TRACY","EDNA","TIFFANY","CARMEN","ROSA","CINDY","GRACE","WENDY","VICTORIA",

Output ['"MARY"','"..."','"..."',..]

Upvotes: 0

Views: 72

Answers (4)

Code-Apprentice
Code-Apprentice

Reputation: 83587

with open("p022_names.txt","r") as f:
    for line in f:
        name=[str(v) for v in line.split(",")]
name.sort()
print(name)

I assume that you see something like this:

['"MARY"','"PATRICIA"'...]

If I understand your question correctly, you want to remove both the ' and teh " characters. The ' characters are here to indicate that the elements of the list are strings. You do not have any of these in your actual data. You only need to remove the double quotes. You don't need to worry about the single quotes. The only quotes you have are the " ones that were read from the file.

Also, you can simplify

name=[str(v) for v in line.split(",")]

to

name = line.split(",")

Upvotes: -1

Andrey Semakin
Andrey Semakin

Reputation: 2795

Here is my solution. It supports multiple names in line separated by comma, also multiple lines in file.

names = []
with open('p022_names.txt') as f:
    for line in f:
        names.extend(v.lower().strip('"').strip("'") for v in line.split(","))

names.sort()

for name in names:
    tot = sum(ord(letter)-96 for letter in name)
    print("{0}: {1}".format(name, tot))

Upvotes: 1

Qwerty
Qwerty

Reputation: 1267

If you want to remove these characters from your string you could probably do something like this:

s = s.replace("\"","")

This replaces all instances of " with an empty string.

If you only want them to be removed on the ends of the string, you can do this

s = s.strip("\"")

If you want to make sure you're only removing one quote from each end, you can do something like this:

if s.startswith("\""):
    s = s[1:]
elif s.endswith("\""):
    s = s[:-1]

If you know the string will always have the quotes, you can simplify it to this:

s = s[1:-1]

However this file appears to be a csv file, and it is much better to parse these files using the csv lib.

Upvotes: 1

curlpipesudobash
curlpipesudobash

Reputation: 721

You can remove the first and last characters from a string in Python by:

name = "'COLIN'"
print(name[1:-1])

This will print COLIN instead of 'COLIN'

Upvotes: 1

Related Questions