0

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
Aran-Fey
44.1k13 gold badges113 silver badges161 bronze badges
asked Aug 28, 2018 at 15:38

4 Answers 4

2

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

answered Aug 28, 2018 at 15:48
Sign up to request clarification or add additional context in comments.

Comments

0

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

answered Aug 28, 2018 at 15:48

Comments

0

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 i.e. readlines() or an actual csv file csv.reader(filename, delimiter="") from library csv i.e. 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.

answered Aug 28, 2018 at 17:47

Comments

0

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
answered Aug 28, 2018 at 15:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.