2016-11-29 200 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語句 - 如果沒有中斷,它會自動繼續。

我也會確定哪個「重複」部分不起作用 - 你有幾個看起來相同的東西,也許把一個數字放在重複問題中。

我不知道你有什麼邏輯問題,但希望上面的幫助你找到它。