Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from collections import OrderedDict
- # find and log comments and more than one blank line in a row
- with open('test.ini', 'r') as f:
- content = f.readlines()
- test = ('#', ';')
- blank = False
- # an ordered dictionary must be used to insert from the start of the file
- # otherwise the insertion points will be off if you use a dict
- comments = OrderedDict() # create the ordered dictionary
- for index, line in enumerate(content):
- if line.startswith(test): # a comment
- comments[index] = line
- if line.strip() == '':
- if blank: # second blank line
- comments[index] = line
- blank = True
- else:
- blank = False
- for key, value in comments.items():
- print(key, value)
Add Comment
Please, Sign In to add comment