Python数据导出/导入到Excel文件/从Excel文件导入数据

2024-05-14 20:59:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python3.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 )

Tags: orthenameinputsearchifhererp
2条回答

我不确定这是否是一个复制问题,但对于最后的if语句,缩进似乎是错误的。除此之外,如果代码更易于阅读,那么调试逻辑问题会更容易——例如,在python中可以使用许多快捷方式来代替

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

你可以用

^{pr2}$

此外,您不需要在while结束时使用continue语句—如果没有中断,它将自动继续。在

我还要确定哪个“重复”部分不起作用-你有几个看起来一样的,也许在重复问题中加上一个数字。在

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

我给出了一个在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文件

^{pr2}$

网上有很多其他的例子,如果你需要更简单的例子,你可以看看。在

相关问题 更多 >

    热门问题