python - Searching a specific columns of a table for not matching items -


with open("test.txt", "r") test:     reader = csv.reader(test, delimiter="\t")     writer = csv.writer(table, delimiter="\t")     row in reader:        field in row:             if field not in keywords:                 writer.writerow(row)                 break 

it seems code writes out every row multiple times. guess looks every single field in each column. how can specify single column?

so code using right , seems misses few rows keyword not present in column.

table = open("table.txt", "w") open("test.txt", "r") test:     reader = csv.reader(test, delimiter="\t")     writer = csv.writer(table, delimiter="\t")     row in reader:         if all(field not in keywords field in row):             writer.writerow(row) 

you can use zip columns then.you can use generator expression within all function checking elements mett condition :

with open("test.txt", "r") spenn,open("test.txt", "r") table:     reader = zip(*csv.reader(spenn, delimiter="\t"))     writer = csv.writer(table, delimiter="\t")     row in reader:         if all(field not in keywords field in row):             writer.writerow(row) 

but if want write rows meet condition can use following code :

with open("test.txt", "r") spenn,open("test.txt", "r") table:     reader = csv.reader(spenn, delimiter="\t")     writer = csv.writer(table, delimiter="\t")     row in reader:         if all(field not in keywords field in row):             writer.writerow(row) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -