我在Python 3.5中编写了以下代码。 问题是我希望从用户获取的值存储在excel中,当程序重新启动时,它会自动从保存的excel文件中读取所有数据。我相信我有办法做到这一点。请指导我。我几乎是Python的初学者。
另请检查代码是否有错误。它有时会完美地运行,有时当它要求重复时,我说不,它仍然会重复。
name=[]
roll=[]
grade=[]
omarks=[]
tmarks=[]
p=-1
while True:
st=input("Do You want to add a Data or Search the Existing Data? \n1=Search \n2=Add \n3=Print All Data???\n")
while st=="3" or st=="P" or st=="Print" or st=="print" or st=="p":
for i in range (len(name)):
print ("Name of the Student: ",(name[i]))
print ("Roll Number:",(roll[i]))
print ("Class of the Student:",(grade[i]))
print (" ")
rpp=input("Repeat?? Y?N: ")
if rpp=="Y" or rpp=="y" or rpp== "Yes" or rpp=="YES" or rpp=="yes" or rpp=="1":
continue
if rpp=="N" or rpp=="n" or rpp== "No" or rpp=="NO" or rpp=="no" or rpp=="0":
break
while st=="2" or st=="A" or st=="Add" or st=="add" or st=="a":
nm=str(input("Enter The Name Here: "))
rl=int(input("Enter The Roll Number Here: "))
gr=int(input("Enter The Class Number Here: "))
om=int(input("Enter the Obtained Marks Here: "))
tm=int(input("Enter the Total Marks Here: "))
name.append(nm)
roll.append(rl)
grade.append(gr)
omarks.append(om)
tmarks.append(tm)
rpa=input("Add Another?? Y?N: ")
if rpa=="Y" or rpa=="y" or rpa== "Yes" or rpa=="YES" or rpa=="yes" or rpa=="1":
continue
if rpa=="N" or rpa=="n" or rpa== "No" or rpa=="NO" or rpa=="no" or rpa=="0":
break
while st=="1" or st=="S" or st=="Search" or st=="s" or st== "search":
rn=int(input("Enter the Roll Number Here: "))
for i in range (len(roll)):
if rn==roll[i]:
#p=i
print ("Name of the Student: ",name[i])
print ("Class of the Student: ",grade[i])
print ("Obtained Marks: ",omarks[i])
print ("Total Marks: ",tmarks[i])
else:
print ("The Roll Number Could not be Found! Search Again or Add the Data.")
rps=input("Search Again?? Y?N: ")
if rps=="Y" or rps=="y" or rps== "Yes" or rps=="YES" or rps=="yes" or rps=="1":
continue
elif rps=="N" or rps=="n" or rps== "No" or rps=="NO" or rps=="no" or rps=="0":
break
rp=input("Repeat The Program?? Y?N: ")
if rp=="Y" or rp=="y" or rp== "Yes" or rp=="YES" or rp=="yes" or rp==1:
continue
if rp=="N" or rp=="n" or rp== "No" or rp=="NO" or rp=="no" or rp==0:
break
try:
untrusted.execute()
except: # catch *all* exceptions
e = sys.exc_info()[0]
write_to_page( "<p>Error: %s</p>" % e )
答案 0 :(得分:0)
我不确定它是否是副本问题,但最终if语句的缩进似乎是错误的。除此之外,如果您的代码更易于阅读,那么调试逻辑问题会更容易 - 您可以在此处使用python中的许多快捷方式,例如代替
E1
你可以使用
if rpa=="Y" or rpa=="y" or rpa== "Yes" or rpa=="YES" or rpa=="yes" or rpa=="1":
此外,您不需要在一段时间内使用continue语句 - 如果没有中断,它将自动继续。
我还要确定哪个“重复”部分不起作用 - 你有几个看起来相同,也许在重复问题中加上一个数字。
我不知道你遇到了什么逻辑问题,但希望上面有助于你找到它。
答案 1 :(得分:-1)
我给出了一个在python中读取和写入excel文件的例子。请注意,您可以在python中使用许多替代程序包来读取和写入Excel文件。查看所有可用包的here。我正在使用xlrd和xlwt给出一个示例,我通常在读取和写入excel文件时使用它。
从Excel文件中读取:
import xlrd
fname = 'Cad Data Mar 2014.xlsx'
# Open the workbook
xl_workbook = xlrd.open_workbook(fname)
# List sheet names, and pull a sheet by name
sheet_names = xl_workbook.sheet_names()
print('Sheet Names', sheet_names)
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
# Or grab the first sheet by index
# (sheets are zero-indexed)
xl_sheet = xl_workbook.sheet_by_index(0)
print ('Sheet name: %s' % xl_sheet.name)
# Pull the first row by index
# (rows/columns are also zero-indexed)
row = xl_sheet.row(0) # 1st row
# Print 1st row values and types
from xlrd.sheet import ctype_text
print('(Column #) type:value')
for idx, cell_obj in enumerate(row):
cell_type_str = ctype_text.get(cell_obj.ctype, 'unknown type')
print('(%s) %s %s' % (idx, cell_type_str, cell_obj.value))
# Print all values, iterating through rows and columns
num_cols = xl_sheet.ncols # Number of columns
for row_idx in range(0, xl_sheet.nrows): # Iterate through rows
print ('-'*40)
print ('Row: %s' % row_idx) # Print row number
for col_idx in range(0, num_cols): # Iterate through columns
cell_obj = xl_sheet.cell(row_idx, col_idx) # Get cell object by row, col
print ('Column: [%s] cell_obj: [%s]' % (col_idx, cell_obj))
写入Excel文件:
import xlwt
# Data that we want to write to an Excel file
DATA = (("The Essential Calvin and Hobbes", 1988,),
("The Authoritative Calvin and Hobbes", 1990,),
("The Indispensable Calvin and Hobbes", 1992,),
("Attack of the Deranged Mutant Killer Monster Snow Goons", 1992,),
("The Days Are Just Packed", 1993,),
("Homicidal Psycho Jungle Cat", 1994,),
("There's Treasure Everywhere", 1996,),
("It's a Magical World", 1996,),)
# Open the workbook
wb = xlwt.Workbook()
# Add a sheet to the workbook
ws = wb.add_sheet("My Sheet")
# Print all values, iterating through rows and columns
for i, row in enumerate(DATA):
for j, col in enumerate(row):
ws.write(i, j, col)
# Set the width for 1st column in the workbook
# You can skip this line and see what happens!
ws.col(0).width = 256 * max([len(row[0]) for row in DATA])
# Save the workbook
wb.save("myworkbook.xls")
如果您需要更多简短的示例,您可以在网上找到很多其他示例。