menu and file
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sat Dec 26 16:21:07 EST 2009
On Sat, 26 Dec 2009 09:52:12 -0800, hong zhang wrote:
> The question is I want to load a
> text file and bring it to screen like
> 1 23 0x4530
> 2 42 0x8790
> 3 75 0x7684
>
> Then if I want to select 0x4530, then I simple select 1 and a handler
> should get 0x4530 as input to process.
Break the problem into small pieces:
(1) Read data from a text file.
(2) Print it to the screen.
(3) Wait for user input.
(4) Call a handler.
Untested:
# Read data from a text file.
# We assume the text file contains one or more lines.
# Each line contains two words, a decimal number and a hex number.
f = open("my-text-file.txt", "r")
lines = []
for line in f:
a, b = line.split()
dec_num = int(a)
hex_num = int(b, 16)
lines.append( (dec_num, hex_num) )
f.close()
# Print the menu to the screen.
print "Menu:"
for i, item in enumerate(lines):
a, b = item
print "%d %d 0x%x" % (i+1, a, b)
# Wait for user input.
t = raw_input("Enter a line number 1-%d: " % len(lines))
t = t.strip()
n = int(t) - 1
# Call a handler.
item = lines[n]
b = item[1]
result = handler(b)
--
Steven
More information about the Python-list
mailing list