Skip to content

Instantly share code, notes, and snippets.

@bravegnu
Last active December 22, 2015 10:02
Show Gist options
  • Save bravegnu/03acfa2473c97911d252 to your computer and use it in GitHub Desktop.
Save bravegnu/03acfa2473c97911d252 to your computer and use it in GitHub Desktop.
Find and Replace First Dot.
matching = {
'{': '}',
'(': ')'
}
def find_first_dot(line):
paren_stack = []
for pos, ch in enumerate(line):
if ch in matching:
paren_stack.append(ch)
else:
if paren_stack:
if ch == matching[paren_stack[-1]]:
paren_stack.pop()
else:
if ch == '.':
return pos
return -1
for line in open("sample-text.txt"):
print line
pos = find_first_dot(line)
if pos != -1:
line = list(line)
line[pos] = '#'
line = "".join(line)
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment