i have got some set of csv i auto generate and i need advise on the following:
- i have three labels on the csv ( Serial Number, Starting Code, Key) see the image below
- 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']
- 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
CodePudding user response:
What I understood from your problem is you want to search for a value from the CSV column name "serial number" and print the next rows of the corresponding serial number. Please correct me if I have misunderstood anything.
here is a demo code for this:
import csv
out_list = []
output_dict = {}
file = 'new.csv'
search = input("Search Serial number here: ")
with open(file, 'r') as my_file:
reader = csv.reader(my_file)
next(reader)
for row in reader:
if row[0] == search:
print(f"Congratulation ! your input is {search} and match with {search}")
out_list = [row[0], row[1], row[2]]
output_dict = {"serial_number": row[0], "starting_code": row[1], "key": row[2]}
print(f"your input list values {out_list}\n")
print(f"your matching dictionary {output_dict}")
the output will be like this:
Search Serial number here: KB57000001006
Congratulation ! your input is KB57000001006 and match with KB57000001006 in
your input list values ['KB57000001006', '22442', 'dfdfdf']
your matching dictionary {'serial_number': 'KB57000001006', 'starting_code': '22442', 'key': 'dfdfdf'}