0

I've a list with only one element, which in itself is a list of elements.

str = '"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL"'

Now, I want each individual name in that string to included in another list. For that, I understand that I have to split each name individually and then append them with the desired list. Here's the code I wrote for the same:

str = '"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL"'
li = str.split()
c = li[0]
ip = []
start = c.find('"')
final = c.find('"', start+1)
def iter(start, final):
    e = c[start+1:final]
    ip.append(e)
nstart = c.find('"', final+1)
nfinal = c.find('"', nstart+1)
if(nstart == -1 or nfinal == -1):
    print ip
else:
    iter(nstart, nfinal)

However, I don't get anything as output. 'ip' is the list where I intend to store all the names, individually. What seems to be the problem with my code?

4
  • 4
    namelist = nameStr [1:-1].split('","') ? Commented Apr 5, 2014 at 22:56
  • Where did this string come from? That looks a lot like a row of a CSV file. Commented Apr 5, 2014 at 23:00
  • I got that string from a file I'm working with. Commented Apr 6, 2014 at 6:56
  • The point is that if it's a CSV file, there's an entire built-in module designed to read them. Commented Apr 6, 2014 at 17:59

3 Answers 3

2

Your code is far more complex than necessary. You could use the comment, or do:

foo = '"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL"'
output = foo.replace('"','').split(",")
Sign up to request clarification or add additional context in comments.

Comments

2

First, calling something str is bad, as str is a builtin function in python.

Second, you can do it with the following:

in_list_string = '"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL"'
out_list= [element.strip('"') for element in in_list_string.split(',')]

Comments

2

Use the csv module for csv data:

>>> import csv
>>> from StringIO import StringIO
>>> dont_call_me_str='"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL"'
>>> list(csv.reader(StringIO(dont_call_me_str)))
[['MARY', 'PATRICIA', 'LINDA', 'BARBARA', 'ELIZABETH', 'JENNIFER', 'MARIA', 'SUSAN', 'MARGARET', 'DOROTHY', 'LISA', 'NANCY', 'KAREN', 'BETTY', 'HELEN', 'SANDRA', 'DONNA', 'CAROL']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.