Python for loop-stripping行随用

Python for loop-stripping行随用,python,for-loop,strip,Python,For Loop,Strip,我试图剥离一行代码,以便只保存末尾的注释。因为符号可以包含在“标记中,为此,我尝试循环遍历”标记的行捕捉对,以便忽略中的任何标记“标记。当我在下面的代码上使用代码可视化工具时,在第二个for循环之后,它似乎开始处理s,就好像它刚刚去掉了第一个“标记一样。我看不出我在这里做错了什么,因为我在第19行包含的print语句显示s已经在第二个之后被去掉了。”,但当代码返回顶部时,它会从第一个“之后再次开始循环。知道我在这里做错了什么吗 s = '("8# " + str" #9 " + line) #l

我试图剥离一行代码,以便只保存末尾的注释。因为
符号可以包含在
标记中,为此,我尝试循环遍历
标记的行捕捉对,以便忽略
中的任何
标记“
标记。当我在下面的代码上使用代码可视化工具时,在第二个for循环之后,它似乎开始处理
s
,就好像它刚刚去掉了第一个
标记一样。我看不出我在这里做错了什么,因为我在第19行包含的print语句显示
s
已经在第二个
之后被去掉了。”
,但当代码返回顶部时,它会从第一个
之后再次开始循环。知道我在这里做错了什么吗

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)

使用正则表达式更容易删除带引号的字符串:

import re
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
pattern = r'"[^"]*"'
s = re.sub(pattern, '', s)
print s[s.index('#'):]
输出:

#lots of hash(#) symbols here

使用正则表达式更容易删除带引号的字符串:

import re
s = '("8# " + str" #9 " + line) #lots of hash(#) symbols here'
pattern = r'"[^"]*"'
s = re.sub(pattern, '', s)
print s[s.index('#'):]
输出:

#lots of hash(#) symbols here

如果我正确理解了你的问题,你只想保留最后的评论(这里有很多散列(#)符号)。 为此,不需要嵌套for循环

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)

如果我正确理解了你的问题,你只想保留最后的评论(这里有很多散列(#)符号)。 为此,不需要嵌套for循环

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)

您的代码过于复杂,因此我建议您使用另一种方法来查找注释,如前面提到的正则表达式或我提出的注释

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)

您的代码过于复杂,因此我建议您使用另一种方法来查找注释,如前面提到的正则表达式或我提出的注释

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)

请检查此处显示的代码是否与IDE或文本编辑器中的代码完全相同?问题可能在于某些行的缩进,这在Python中非常重要。请检查此处显示的代码是否与IDE或文本编辑器中的代码完全相同?问题可能在于某些行的缩进es,这在Python中非常重要。说明:这将删除
s
中引号内的所有内容,然后从剩余的第一个
#
中删除所有内容。说明:这将删除
s
中引号内的所有内容,然后从剩余的第一个
#
中删除所有内容。