- 论坛徽章:
- 0
|
我用的是ActiveState Komodo编辑器,由于程序比较复杂,用一个简单的例子来说明.
例如下面这段程序:
# -*- coding: utf8 -*-
#!/usr/bin/env python
import sys
def getName():
name=int(raw_input("choose name:"))
return name
def getAtt():
att=raw_input("Input attribute:")
return att
def display(name,att):
raw=record[name].split()
if att == "diyinian":
print raw[1].ljust(10)
elif att == "第二年":
print raw[2].ljust(10)
elif att == "第三年":
print raw[3].ljust(10)
try:
file=open("路径\\excel.xls","r")
except IOError:
print >> sys.stderr,"File could not be opened"
sys.exit(1)
print "choose:"
print "1 - 名称1"
print "2 - 名称2"
print "3 - 名称3"
print "4 - 名称4"
print "5 - 名称5"
name=getName()
att=getAtt()
record=file.readlines()
display(name,att)
file.close()
表文件是:excel.xls
数值 diyinian 第二年 第三年
名称1 1 10 10
名称2 2 20 20
名称3 3 30 30
名称4 1 7 12
名称5 2 14 24
运行程序:
choose:
1 - 名称1
2 - 名称2
3 - 名称3
4 - 名称4
5 - 名称5
choose name:1
Input attribute:第二年
(以上出现空白行)
若如下运行:
choose:
1 - 名称1
2 - 名称2
3 - 名称3
4 - 名称4
5 - 名称5
choose name:1
Input attribute:diyinian
1
不知道我说清楚了吗?
因为表中大部分是中文,所以不可能全部用字母来代替。
请指教
谢谢
[ 本帖最后由 nicki0923 于 2006-3-6 20:12 编辑 ] |
|