Skip to content

Instantly share code, notes, and snippets.

@guanqun
Last active September 4, 2015 13:55
Show Gist options
  • Save guanqun/332803 to your computer and use it in GitHub Desktop.
Save guanqun/332803 to your computer and use it in GitHub Desktop.
# read lines
lines = open('file-name.txt').readlines()
# change a specific line
# e.g. using regular expression
p = re.compile(...)
for i in range(len(lines)):
m = p.match(lines[i])
if m:
# m.group(0) or m.group(1)
# MODIFY!
# write back to this file
f = open('file-name.txt')
f.writelines(lines)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment