Reputation:
I am trying to strip a line of code so that only the comment at the end is saved. Because #
signs can be included within ""
marks, to do this I am trying to cycle through the line catching pairs of "
marks so that it ignores any #
marks within ""
marks. When I use a code visualiser on my code below, after the second for loop it seems to go pack to processing s
as if it has just stripped the first "
mark. I can't see what I'm doing wrong here, because the print statement I have included on line 19 shows that s
has been stripped to after the second "
, but when the code returns to the top, it starts cycling again from after the first "
. Any idea of what I am doing wrong here?
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
quoteCount = 0
for char in s:
if quoteCount%2 == 0:
if char == '#':
s = s[s.index('#'):]
break
if char == '"':
quoteCount = quoteCount + 1
s = s[s.index('"'):]
s = s.lstrip('"')
for char in s:
if char == '"':
quoteCount = quoteCount + 1
s = s[s.index('"'):]
s = s.lstrip('"')
print(s)
break
print(s)
Upvotes: 0
Views: 85
Reputation: 56
Your code is overly complicated so I suggest you use an alternative method to finding the comment like the already mentioned regex one or the one I came up with.
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
s = s[s.rfind('"') + 1:] # Get to the last quotation mark
if s.find('#') >= 0: # The first # sign should start the comment if there is one
s = s[s.find('#'):]
else:
s = '' # No comment was found
print(s)
Upvotes: 0
Reputation: 32
If I understand your question correctly you only want to keep the last comment (#lots of hash(#) symbols here). To do this you don't need the nested for loop.
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
quoteCount = 0
for char in s:
if quoteCount%2 == 0:
if char == '#':
s = s[s.index('#'):]
break
if char == '"':
quoteCount = quoteCount + 1
s = s[s.index('"'):]
s = s.lstrip('"')
print(s)
Upvotes: 2
Reputation: 10865
Easier to remove the quoted strings with a regular expression:
import re
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
pattern = r'"[^"]*"'
s = re.sub(pattern, '', s)
print s[s.index('#'):]
Output:
#lots of hash(#) symbols here
Upvotes: 0