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