
February 24th, 2004, 03:02 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Just another example, based on sfb's with a few minor differences. Mainly to show the "if 'key' in dict" statment and stop unpacking errors.
1. only splitting on the first colon, this helps stop unpacking errors resulting from lines containing more than one colon.
2. uses if statments instead of try-except blocks. Just a personal preferance
Code:
#!/usr/bin/env python
parts = {}
for line in file('file.txt'):
try:
x, y = line.split(':', 1)
parts[x] = y
except:
pass
value = raw_input('Enter a value')
if value in parts:
print value, 'links to', parts[value]
else:
print value, 'not found in parts'
Note: this does requite Python 2.3.
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : February 24th, 2004 at 03:14 PM.
|