简体   繁体   English

如何使用Python 2.7从字符串列表中替换字符串的一部分

[英]How to replace part of a string from a list of strings using Python 2.7

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. 我有一个从csv文件读取的唯一文件路径的列表,我想通过多种方法来过滤此列表。 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循环:

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 这将检查列表中的any单词是否出现在路径中:如果没有出现,则将其打印出来

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. 如果您的列表较长,那么我将遍历每个作品的列表,并使用pop删除包含该单词的所有路径。 Documentation for pop, list.pop(i) https://docs.python.org/3.1/tutorial/datastructures.html 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. 如果没有csv文件样本,这很难衡量,也许下次再添加。 :) 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. :)我也不确定在读取文本文件(即readlines()或从library csv读取实际的csv文件csv.reader(filename, delimiter="")之间是否混淆csv.reader(filename, delimiter="") ,即import csv将数据读取为列和行。 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: 如果您希望像readlines()一样将其读取为文本文件,则需要执行以下操作:

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. 如果不确定情况如何,可以在其中插入一条print线,以查看循环的每个阶段的输出等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM