Guest User

Untitled

a guest
Sep 25th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from collections import OrderedDict
  4.  
  5. # find and log comments and more than one blank line in a row
  6. with open('test.ini', 'r') as f:
  7. content = f.readlines()
  8.  
  9. test = ('#', ';')
  10. blank = False
  11. # an ordered dictionary must be used to insert from the start of the file
  12. # otherwise the insertion points will be off if you use a dict
  13. comments = OrderedDict() # create the ordered dictionary
  14. for index, line in enumerate(content):
  15. if line.startswith(test): # a comment
  16. comments[index] = line
  17. if line.strip() == '':
  18. if blank: # second blank line
  19. comments[index] = line
  20. blank = True
  21. else:
  22. blank = False
  23.  
  24. for key, value in comments.items():
  25. print(key, value)
  26.  
Add Comment
Please, Sign In to add comment