python - Why is no result in this code? -
emp.csv
index empno ename job mgr hiredate sal comm deptno 0, 7839, king, president, 0, 1981-11-17, 5000, 0, 10 1, 7698, blake, manager, 7839, 1981-05-01, 2850, 0, 30 2, 7782, clark, manager, 7839, 1981-05-09, 2450, 0, 10 3, 7566, jones, manager, 7839, 1981-04-01, 2975, 0, 20 4, 7654, martin, salesman, 7698, 1981-09-10, 1250, 1400, 30 5, 7499, allen, salesman, 7698, 1981-02-11, 1600 300, 30 6, 7844, turner, salesman, 7698, 1981-08-21, 1500, 0, 30 7, 7900, james, clerk, 7698, 1981-12-11, 950, 0, 30 8, 7521, ward, salesman, 7698, 1981-02-23, 1250, 500, 30 9, 7902, ford, analyst, 7566, 1981-12-11, 3000, 0, 20 10, 7369, smith, clerk, 7902, 1980-12-09, 800, 0, 20 11, 7788, scott, analyst, 7566 1982-12-22, 3000, 0, 20 12, 7876, adams, clerk, 7788, 1983-01-15, 1100, 0, 20 13, 7934, miller, clerk, 7782, 1982-01-11, 1300, 0, 10
i can below result in code.
import csv job = input('enter job : ' ) open("d:\r data\emp2.csv", 'r') f: reader = csv.reader(f, delimiter=',') row in reader: if all(s in job s in row[2]): print (row[1],row[5],row[2])
result :
enter job : salesman,analyst martin 1250 salesman allen 1600 salesman turner 1500 salesman ward 1250 salesman ford 3000 analyst scott 3000 analyst
but can't above result in below code. no result.
i want change "job = 'salesman','analyst' " of below code.
import csv job = 'salesman','analyst' open("d:\r data\emp2.csv", 'r') f: reader = csv.reader(f, delimiter=',') row in reader: if all(s in job s in row[2]): print (row[1],row[5],row[2])
how can below result in above code ?
martin 1250 salesman allen 1600 salesman turner 1500 salesman ward 1250 salesman ford 3000 analyst scott 3000 analyst
job
has kind of container used in
keyword. not mean cannot have user input. example can this:
keywords = input('provide search strings separated commas (','):\t').split(',')
furthermore, csv file reading contains slashes in path. slashes used escape characters in strings file not read @ in case since path throws error. use with open(r"d:\r data\emp2.csv", 'r') f:
instead. notice r
in front of string. tells python read is, disregarding escapes. alternativelly, can use double slashes (thus escaping slashes themselves) like: with open("d:\\r data\\emp2.csv", 'r') f:
.
additionally, indexes use wrong. python uses 0 index! have check row[3]
keywords , return row[2], row[6], row[3]
based on desired output, not check row[2] , return 1,5 & 2.
finally, check wrong. want see if row[3]
off strings supplied. can if row[3] in job:
putting have:
import csv job = input('provide search strings separated commas (','):\t').split(',') open(r"c:\users\evkouni\desktop\test.csv", 'r') f: reader = csv.reader(f, delimiter=',') row in reader: if row[3] in job: print(row[2], row[6], row[3])
demo:
runing above provided *.csv file (you missing commas) returns:
#martin 1250 salesman #allen 1600 salesman #turner 1500 salesman #ward 1250 salesman #ford 3000 analyst #scott 3000 analyst
Comments
Post a Comment