2016-11-29 91 views
-1

我已经在Python 3.5中编写了下面的代码。 问题是我想从用户获取的值存储在Excel中,当程序重新启动时,它会自动读取保存的Excel文件中的所有数据。我确信有一些方法可以做到这一点。请指导我。我几乎是Python的初学者。Python数据导出/导入到/从Excel文件

也请检查错误代码。它有时会完美运行,有时它会重复,我说不,它仍然会重复。

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

将rp与1或0进行比较之前的一件事情,将rp转换为整数作为'int(rp)',否则它将不适用于输入1或0. –

+0

实际上命名变量有意义的名称可能也有帮助... – Lunaweaver

回答

-1

我给出了一个从python读写excel文件的例子。请注意,您可以在Python中使用许多替代软件包来读取和写入excel文件。查看所有可用软件包的here。我给出了一个使用xlrdxlwt的例子,我通常在读取和写入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") 

有很多关于网络上的其他例子,你可以看到,如果你需要更多的简短的例子。

+0

Wasi bhai,这不是解决方案。 – r0xette

+1

我知道这不是一个解决方案,但OP可以从这个例子中解决他的问题。我相信:举一个例子来学习,然后修改例子来满足需要。这样,OP就可以同时学习和解决问题。在SO中,我们帮助程序员解决他们的问题,但是SO不是解决方案提供者! –

0

我不确定它是否是复制问题,但最终的if语句的缩进似乎是错误的。以外的是它会更容易调试你的逻辑问题,如果你的代码更容易阅读 - 有Python中的许多快捷键,你可以在这里使用,例如,而不是

if rpa=="Y" or rpa=="y" or rpa== "Yes" or rpa=="YES" or rpa=="yes" or rpa=="1": 

可以使用

if rpa.lower() in ['y', 'yes', '1']: 

另外,在最后一段时间不需要continue语句 - 如果没有中断,它会自动继续。

我也会确定哪个“重复”部分不起作用 - 你有几个看起来相同的东西,也许把一个数字放在重复问题中。

我不知道你有什么逻辑问题,但希望上面的帮助你找到它。