繁体   English   中英

搜索CSV文件重复错误

[英]Searching a CSV file repeat error

我正在使用日期中的月份搜索csv文件:

Jackson,Samantha,2 Heather Row,Basingstoke,RG21 3SD,01256 135434,23/04/1973,sam.jackson@hotmail.com
Vickers,Jonathan,18 Saville Gardens,Reading,RG3 5FH,01196 678254,04/02/1965,the_man@btinternet.com
Morris,Sally,The Old Lodge, Hook,RG23 5RD,01256 728443,19/02/1975,smorris@fgh.co.uk
Cobbly,Harry,345 The High Street,Guildford,GU2 4KJ,01458 288763,30/03/1960,harry.cobbly@somewhere.org.uk
Khan,Jasmine,36 Hever Avenue,Edenbridge,TN34 4FG,01569 276524,28/02/1980,jas.khan@hotmail.com
Vickers,Harriet,45 Sage Gardens,Brighton,BN3 2FG,01675 662554,04/04/1968,harriet.vickers@btinternet.com

有一些问题,例如; 没有显示所有相关人员,则重试它不会起作用并产生错误:

   1. Surname
2. D.O.B
3. Quit
Please select an option: 2
Please enter the birth month in a two digit format e.g. 02: 12
Month not found.
Please enter the birth month in a two digit format e.g. 02: 02
Month not found.
Please enter the birth month in a two digit format e.g. 02: 02
Month not found.
Please enter the birth month in a two digit format e.g. 02: 03
Month not found.
Please enter the birth month in a two digit format e.g. 02: 12
Month not found.
Please enter the birth month in a two digit format e.g. 02: 01
Month not found.
Please enter the birth month in a two digit format e.g. 02: 
Must be a number
Please enter the birth month in a two digit format e.g. 02: 04
Month not found.
Please enter the birth month in a two digit format e.g. 02: 50
Must be 1-12
Please enter the birth month in a two digit format e.g. 02: 

这是代码:

def input_month():
    addrsBk
    while True:
        try:
            month = int(input("Please enter the birth month in a two digit format e.g. 02: "))
        except ValueError:
            print("Must be a number")
        else:
            if month in range(1, 13):
                return month
            print("Must be 1-12")



def DOB_search(BkRdr):
    addrsBk
    while True:
        search_month = input_month()
        addrsBk
        for row in BkRdr:
            DOB = row[6]
            day,month,year = DOB.split("/")
            if search_month == int(month): 
                surname = row[0]
                firstname = row[1]
                print(firstname, " ",surname)
                return"".join((firstname, surname))
                addrsBk.close
        print("Month not found.")

好吧,问题在于第一次执行for row in BkRdr: ,文件耗尽,指针为EOF(文件末尾),没有更多的行可读取。 然后,第二次通过for row in BkRdr:for row in BkRdr:不再执行任何块。

若要解决此问题,您必须使用.seek(0)属性将文件指针指向文件的.seek(0)

如果您已打开csv文件,如下所示:

myFile = open('file_01.csv', mode='r')
BkRdr = csv.reader(myFile, delimiter=',')

您可以使用以下方式修复代码:

for row in BkRdr:
    DOB = row[6]
    day,month,year = DOB.split("/")
    if search_month == int(month): 
        surname = row[0]
        firstname = row[1]
        print(firstname, " ",surname)
        return"".join((firstname, surname))
        addrsBk.close
print("Month not found.")
myFile.seek(0)

<file>.seek(int)将文件行指针放在int编号的位置,在这种情况下,指向文件的开头(BOF)。

在任何情况下,这都不是csv文件的问题,而是文件对象的问题。 您可以阅读以下内容:

文件对象的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM