Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 1.03 KB | Hits: 71 | Expires: Never
Copy text to clipboard
  1. rik@hogbarn ~ $ python
  2. Python 2.6.4 (r264:75706, Mar  1 2010, 17:04:42)
  3. [GCC 4.4.2] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import os
  6. >>> import sys
  7. >>> with open('/usr/src/linux/Makefile') as file:
  8. ...         head = [file.next() for x in range(4)]
  9. ...         print head
  10. ...         import  string
  11. ...         head = map(string.strip, head)
  12. ...         print head
  13. ...         head = dict(item.split("=") for item in head )
  14. ...         print head
  15. ...         for k,i in head.items():
  16. ...                 print k, i
  17. ...         print head['EXTRAVERSION']
  18. ...
  19. ['VERSION = 2\n', 'PATCHLEVEL = 6\n', 'SUBLEVEL = 33\n', 'EXTRAVERSION = -gentoo\n']
  20. ['VERSION = 2', 'PATCHLEVEL = 6', 'SUBLEVEL = 33', 'EXTRAVERSION = -gentoo']
  21. {'SUBLEVEL ': ' 33', 'EXTRAVERSION ': ' -gentoo', 'VERSION ': ' 2', 'PATCHLEVEL ': ' 6'}
  22. SUBLEVEL   33
  23. EXTRAVERSION   -gentoo
  24. VERSION   2
  25. PATCHLEVEL   6
  26. Traceback (most recent call last):
  27.   File "<stdin>", line 11, in <module>
  28. KeyError: 'EXTRAVERSION'
  29. >>>