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
Post a Comment