How do quickly search through a .csv file in Python

后端 未结 6 990
别跟我提以往
别跟我提以往 2020-12-18 09:10

I\'m reading a 6 million entry .csv file with Python, and I want to be able to search through this file for a particular entry.

Are there any tricks to search the en

6条回答
  •  没有蜡笔的小新
    2020-12-18 09:57

    There is a fairly simple way to do this.Depending on how many columns you want python to print then you may need to add or remove some of the print lines.

    import csv
    search=input('Enter string to search: ')
    stock=open ('FileName.csv', 'wb')
    reader=csv.reader(FileName)
    for row in reader:
        for field in row:
            if field==code:
                print('Record found! \n')
                print(row[0])
                print(row[1])
                print(row[2])
    

    I hope this managed to help.

提交回复
热议问题