python 如何从输入的搜索值中获取特定对象的详细信息?

r1zhe5dt  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(112)

我有一个大学项目,我正在工作,并遇到了一个障碍。我希望该计划能够搜索一个特定的对象在"employee.txt"列表中的基础上,已输入的搜索值。我不允许使用SQL和只有文本文件。我的导师不知道如何解决这个问题,所以我来恳求您的帮助!

    • 我试过**
empList = open("employee.txt")

def add_emp():
    empSurname = txtSurname.get()
    empForename = txtForename.get()
    empPhone = txtPhone.get()
    empGender = comboGender.get()
    empDOB = txtDOB.get()
    empEmail = txtEmail.get()
    empHouseNum = txtHouseNum.get()
    empStreetName = txtStreetName.get()
    empTC = txtTc.get()
    empCounty = txtCounty.get()
    empPostcode = txtPostcode.get()

    Employee(empSurname, empForename, empGender, empPhone, empDOB, empEmail, empHouseNum, empStreetName, empPostcode, empTC, empCounty)

    for i in empList:
        if i.surname == empSurname:
            Details = str(i)
            print(Details)
    • 但是我得到了一个属性错误,它指出"str对象不包含属性'surname'"这是我的雇员文件:**
class Employee():
    def __init__(self, surname, forename, gender, phoneNum, DOB, email, houseNum, streetName, postcode,     tc, county):
        self.surname = surname
        self.forename = forename
        self.phoneNum = phoneNum
        self.DOB = DOB
        self.email = email
        self.houseNum = houseNum
        self.streetName = streetName
        self.postcode = postcode
        self.tc = tc
        self.county = county
       
        CAPL = self.surname.upper()
        CAPF = self.forename.upper()
    
        count = 1
        for i in employeeList:
            if i[:4] == CAPL[:3] + CAPF[:1]:
                count+= 1
            
        empID = CAPL[:3] + CAPF[:1] + str("{:02d}".format(count))
        self.empID = empID
    
        employeeA = open("employee.txt", "a")
        employeeA.write(empID + " " + surname + " " + forename + " " + gender + " " + phoneNum + " " + DOB + " " + email + " " + houseNum + " " + streetName + " " + postcode + " " + tc + " " + county + "\n")
        employeeA.close()

非常感谢你的帮助!

mpbci0fu

mpbci0fu1#

您需要将文件每行的第二个字段与输入的姓氏进行比较。
如果遍历整个文件没有找到匹配项,则可以创建新员工,并将其添加到文件中。

def add_emp():
    empSurname = txtSurname.get()
    empForename = txtForename.get()
    empPhone = txtPhone.get()
    empGender = comboGender.get()
    empDOB = txtDOB.get()
    empEmail = txtEmail.get()
    empHouseNum = txtHouseNum.get()
    empStreetName = txtStreetName.get()
    empTC = txtTc.get()
    empCounty = txtCounty.get()
    empPostcode = txtPostcode.get()

    with open("employee.txt") as empList:
        for i in empList:
            fields = i.split()
            if len(fields) < 2:
                continue # skip lines that don't have enough fields
            oldSurname = i[1]
            if oldSurname == empSurname:
                print(f"Employee {empSurname} already exists")
                print(Details)
                break
        else:
            new_emp = Employee(empSurname, empForename, empGender, empPhone, empDOB, empEmail, empHouseNum, empStreetName, empPostcode, empTC, empCounty)
            return new_emp

相关问题