I have a list of unique file paths read from a csv file and I would like to filter this list via a number of ways. One of which is to exclude paths that contain specific words. I have created a list of words but I'm not sure how to use it to filter the paths. The below code doesn't work.
with open("C:\MXD\dataSources.csv") as csvfile:
pathList = csvfile.readlines()
vendMastList = ["Vendor", "vendor", "master", "Master"]
for pth in pathList:
for vendMast in vendMastList:
if vendMast not in pth:
print pth
I think all you need to do is replace the 2nd for loop:
for path in pathList:
if not any(name in path
for name in vendMastList):
print(path)
This checks if any
of the words in the list appear in the path: and if none do, then print it out
With a list that short you could just check for each of them.
for path in pathList:
if not 'Vendor' in path and not 'vendor' in path and \
not 'Master' in path and not 'Master' in path:
print path
If your list was longer then I would run through the list of each work and use pop to remove any path that contain the word. Documentation for pop, list.pop(i) https://docs.python.org/3.1/tutorial/datastructures.html
Since you need to consider that none of words is contained in path, using a flag to record whether some word is contained in path is the most intuitive approach. Fix it:
with open("C:\MXD\dataSources.csv") as csvfile:
pathList = csvfile.readlines()
vendMastList = ["Vendor", "vendor", "master", "Master"]
for pth in pathList:
contained = False
for vendMast in vendMastList:
if vendMast in pth:
contained = True
break
if not contained:
print pth
This is a little hard to gauge without a sample of the csv file, maybe add it next time. :) I am also not sure if you are getting mixed up between reading a text file ie readlines()
or an actual csv file csv.reader(filename, delimiter="")
from library csv
ie import csv
which reads the data as columns and rows. The First line will make up the columns and rest are rows.
If you wish to read it as text file as in readlines()
, then you will want to do something like this:
with open("C:\MXD\dataSources.csv") as csvfile:
pathList = csvfile.read().splitlines() # removes newlines "\n" characters
vendMastList = ["Vendor", "vendor", "master", "Master"]
for line in pathList:
# print(line) # to see what is happening
result = line.split(",")
# print(result) # etc
for i in range(len(result)):
for j in range(len(vendMastList)):
if result[i] != vendMastList[j]:
new_result = result
print(new_result)
csvfile.close # Don't forget to close it :)
If you are unsure how things are going, put a print
line in to see what the output is for every stage of the loop etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.