Python。”需要多个值才能解包“

2024-04-19 20:55:55 发布

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

def store_inFile(Name,Address,Area,Postcode):
    #open the file
    File2 = open("Fines.txt","rt")

    #write the name address and post code to file and save to file
    File2.write(str(Name)+"\n"+str(Address)+"\n   "+str(Area)+"\n      "+str(Postcode));File2.close()

    #set File to open the datatbase
    File =open("DATABASE.txt","rt")
    NumP_Input = str(input("Number plate of person speeding. ").upper())

    #loop each line intil the numberplate gets a match
    for line in File:
        Numberplate,Name,Address,Area,Postcode = line.split("|") #here use whatever you have splitting your database ie , . / ( |
        if NumP_Input.upper() == Numberplate:
            #output name address and number plate
            print("Fine sent to \n \n"+str(Name)+"\n"+str(Address)+"\n   "+str(Area)+"\n      "+str(Postcode));store_inFile(Name,Address,Area,Postcode)

我想从一个数据库中读取,如果用户输入一个与数据库相关联的号码牌,所有的信息(参数)都会被发送到另一个文件中。你知道吗


Tags: andthetostorenameaddresslinearea
1条回答
网友
1楼 · 发布于 2024-04-19 20:55:55

检查您的输入-文件“数据库.txt". 有一个line中没有“|”,因此它不会像代码要求的那样“拆分为五个部分”:

Numberplate,Name,Address,Area,Postcode = line.split("|")

示例:

>>> line = "hello, world"
>>> Numberplate,Name,Address,Area,Postcode = line.split("|")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

相关问题 更多 >