Python Forum
The simplest way to search through Csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The simplest way to search through Csv
#1
Hi there, i have got some set of csv i auto generate and i need advise on the following:

1. i have three labels on the csv ( Serial Number, Starting Code, Key) see the image below
[Image: Csv-toke-arrange.png]

2. from the image above i create
 input("")
to check if the input keyword is equal to any of the column , if the input is equal to any of the column then print the rest of the cells in the row e.g if
 if input == 'KB57000001003', print ('983901181',	'f3e78b3163de11f1dc143082205262e9') and stored 
them in variable x and y  ( x= '983901181' and y = 'f3e78b3163de11f1dc143082205262e9')  
i have try to do this with csv built in functions but can"t see my code below:

 import csv
dict = []
file = 'token.csv'
search = input("Search Serial number here")
inValue = 0
with open(file, 'r') as my_file:
    reader = csv.reader(my_file)
    for row in reader:
        

        ## this is for serial number
        for i in range(len(row)):
            #dict.append(i)
            
            if i == 0:
                print(f'The row number {i} is  {row[i]}')
                dict.append(row[i])
                if row[i]== search:
                      inValue = row[i]
                      print(f" Congratulation ! your input is {search} and match with {search} in ")               
            elif i > 0:
                continue
            elif i == len(row):
                break
        for i in range(len(row)):
            if i >=1:
                dict_1.append(row[i])
            elif i == len(row):
                break


              
          
print('\n')
print(inValue)
print('\n')
print(dict)   
Out:
Search Serial number here: KB57000001006
The row number 0 is  Serial Number
The row number 0 is  KB57000001001
The row number 0 is  KB57000001002
The row number 0 is  KB57000001003
The row number 0 is  KB57000001004
The row number 0 is  KB57000001005
The row number 0 is  KB57000001006
 Congratulation ! your input is KB57000001006 and match with KB57000001006 in 


KB57000001006


 the list in first Column is ['Serial Number', 'KB57000001001', 'KB57000001002', 'KB57000001003', 'KB57000001004', 'KB57000001005', 'KB57000001006']
3. i was only able to scan through the column and get the match but can't print the next two rows in the column, please can you help
Thanks Greatly in advance
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020