I’m a bit confused about a line in Python:
We use Python and a custom function to split a line: we want what is between quotes to be a single entry in the array.
The line is, for example:
"La Jolla Bank, FSB",La Jolla,CA,32423,19-Feb-10,24-Feb-10
So “La Jolla Bank, FSB” should be a single entry in the array.
And I’m not sure to understand this code:
-
The first char is a quote ‘
"‘, so the variable “quote” is set to its inverse, so set to “TRUE”. -
Then we check the comma, AND if quote is set to its inverse, so if quote is TRUE, which is the case when we are inside the quotes.
-
We cut it with
current="", and this is where I don’t understand: we are still between the quotes, so normally we should not cut it now! edit: so and not quote means “false”, and not “the opposite of”, thanks !
Code:
def mysplit (string):
quote = False
retval = []
current = ""
for char in string:
if char == '"':
quote = not quote
elif char == ',' and not quote: #the first coma is still in the quotes, and quote is set to TRUE, so we should not cut current here...
retval.append(current)
current = ""
else:
current += char
retval.append(current)
return retval
You’re viewing it as though both
if char == '"'andelif char == ',' and not quotewere run.However the if statement explicitly makes it so that only one will run.
Either, quote will be inverted OR the
currentvalue will get cut.In the case where the current char is
", then the logic will be called to invert thequoteflag. But the logic to cut the string will not run.In the case where the current char is
,, then the logic for inverting the flag will NOT run, but the logic to cut the string will if thequoteflag is not set.