Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python advance for loop

I am an old dog who used BASIC > 30 years ago. I have run into this scenario in using for loops in python before, but I picked this illustration for my concern about loops:

I would like to parse a long string which includes words in double quotes separated by commas. I can ignore double quotes, but I want the loop to advance here. I don't feel like this is very elegant. I am carrying unnecessary looping baggage. Should I do away with the loop altogether, in which case, is slicing the preferred method, and is there a general rule to apply to the question of using loops or not?

"""
data is the str-type variable
line, despite the name, seems to pull out just one character at a time
(which is not relevant except to confirm my naïveté in python)
"""

for line in data:
    if line.endswith('"'):
        x = True  # doing nothing but advancing the for loop
    elif line.endswith(','):
        #  do something at a comma
    else:
        #  continue the parsing

Edit Example string:

"All","the","world","'s","a","stage","And","all","the","men","and","women","merely","players"
like image 813
Jeptha Avatar asked Mar 17 '16 02:03

Jeptha


3 Answers

I would like to parse a long string which includes words in double quotes separated by commas

Let data be

data = '''"this","is","a","test"'''

Then you can split() on commas

for quote in data.split(','):

I can ignore double quotes

Yes, you can strip() the quotes

    word = quote.strip('"')

Then print

    print(word)

All together

data = '''"this","is","a","test"'''

for quote in data.split(','):
    word = quote.strip('"')
    print(word)

Outputs

this
is
a
test
like image 100
OneCricketeer Avatar answered Nov 19 '22 19:11

OneCricketeer


For your general questions about loops, if you want to parse the string line by line, you can do either:

for line in data.split('\n'):
    …

or

for line in data.splitlines():
    …

long string which includes words in double quotes separated by commas. I can ignore double quotes, but I want the loop to advance here

But after reading your question several times, you never said you actually want to iterate over lines. Instead, you might want to split your strings at the comma:

for element in data.split(','):
    …

and then, if you want to remove the quotes, you can strip them out:

    element.strip('"\'')

edit:

here's a go with your example, to extract each word:

>>> s = '''"All","the","world","'s","a","stage","And","all","the","men","and","women","merely","players"'''
>>> 
>>> for element in s.split(','):
...     element = element.strip('"')
...     print(element)
... 
All
the
world
's
a
stage
And
all
the
men
and
women
merely
players

HTH

like image 42
zmo Avatar answered Nov 19 '22 19:11

zmo


Since data is str the for loop will advance one character at a time. If you want to split the str to lines separated by newline character you can do so by split method that returns a list of lines:

for line in data.split('\n'):
    # do something with line
like image 1
niemmi Avatar answered Nov 19 '22 19:11

niemmi