#load your xlsx file def open_excel(file='grade.xlsx'): try:
data=xls.open_workbook(file) return data except: print 'Please check your file name agian.' #locate targeted table def locate_table():
data = open_excel()
table1 = data.sheets()[0] return table1
#search someone's row def search(id):
table=locate_table() for i in range(2,table.nrows()): if int(table.cell(i,0))==id: return i else: print 'can\'t find targeted object' #calculate by sheet's character def cal_someone():
table=locate_table()
row=search(id)
point=0
credit=0 for j in range(1,table.ncols):
thispoint=table.cell(row,1)
credit+=table.cell(row,j)*thispoint
point+=thispoint
currentgpa=credit/point return currentgpa
def append_result(id,GPA): #let each GPA append this file file('result.txt','w') #创建文件 f=file('result.txt','a')
f.write(id)
f.write('--')
f.write(GPA)
#main function def main():
open_excel()
locate_table()
print 'You should input id to calculate this one\'s GPA' id=input()
search(id)
gpa=cal_someone()
append_result(id,gpa)
2.测试的时候卡在了search这一行,提示如下:
Connected to pydev debugger (build 171.4694.67)
You should input id to calculate this one's GPA
64
Traceback (most recent call last):
File "D:\Program Files\PyCharm Community Edition 2017.1.4\helpers\pydev\pydevd.py", line 1591, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "D:\Program Files\PyCharm Community Edition 2017.1.4\helpers\pydev\pydevd.py", line 1018, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "E:/Pycode+/Tools/GPA.py", line 62, in <module>
main()
File "E:/Pycode+/Tools/GPA.py", line 57, in main
search(id)
File "E:/Pycode+/Tools/GPA.py", line 22, in search
rows=table.nrows()
TypeError: 'int' object is not callable
#Search someone's row
def search(id):
table=locate_table()
for i in range(2,table.nrows):
if table.cell(i,0).value==id:
return i
#Calculate by sheet's character
def cal_someone(id):
table=locate_table()
row=search(id)
point=0
credit=0
for j in range(1,table.ncols):
onepoint=table.cell(1,j).value
credit+=table.cell(row,j).value * onepoint
point+=onepoint
current_GPA=credit/point
return current_GPA
"""
def append_result(id,GPA): #let each GPA append this file
file('result.txt','w') #创建文件
f=file('result.txt','a')
f.write(id)
f.write('--')
f.write(GPA)
"""
#main function
def main():
print 'Input ID to calculate GPA'
oneid=input()
try:
g=cal_someone(oneid)
print g
except StandardError:
print 'Check ID again,Thank!'
#Search someone's row
def search(id):
table=locate_table()
for i in range(2,table.nrows):
if table.cell(i,0).value==id:
return i
#Calculate by sheet's character
def cal_someone(id,assure):
table=locate_table()
row=search(id)
point=0
credit=0
time=1
for j in range(1,table.ncols):
if isinstance(table.cell(row,j).value,float):
if assure=='yes' or 'YES':#acquire have empty
if time<table.ncols:
onepoint=table.cell(1,j).value
credit+=table.cell(row,j).value * onepoint
point+=onepoint
time+=1