如果与列表元素匹配,则从csv文件中删除特定行- python / windows

oxosxuxt  于 2023-02-14  发布在  Python
关注(0)|答案(2)|浏览(125)

我有一个csv文件,每行有一个名称和url(在第一列)。另一方面,我有一个列表,名称来自脚本。我想删除csv文件中包含列表中名称的行。这听起来很简单,但我尝试了几个选项,没有一个有效。
csv格式为:

John Doe, johndoe.blog.com
Jane Doe, janedoe.blog.com
Jim Foe, jimfoe.blog.com

名单如下:

not_ok_name= [John Doe , Jim Foe]

csv文件的输出应为:

Jane Doe, janedoe.blog.com

在最后一次尝试中,我尝试了以下解决方案:

count= 0
while count< len(not_ok_name):
    writer = csv.writer(open('corrected.csv'))
    for row in csv.reader('myfile.csv.csv'):
        if not row[0].startswith(not_ok_name[count]):
            writer.writerow(row)
    writer.close()

由于我还是个新手,我期待一些简单的建议。谢谢。
编辑:以防原始数据可能存在格式问题,我将粘贴以下结果:

print repr(open("myfile.csv", "rb").read())

John Doe ,johndoe.blog.com\r\nJane Doe , janedoe.blog.com

希望这能帮上忙谢谢
编辑2:这里有一段代码完成了部分工作。它删除了一个名字。也许它有助于为整个列表开发一个名字。

reader = csv.reader(open("myfile.csv", "rb"), delimiter=',')
with open('corrected.csv', 'wb') as outfile:
    writer = csv.writer(outfile)
    for line in reader:
        #for item in Names:
        if not any ("Jim Foe" in x for x in line):
            writer.writerow(line)
            print line

再次感谢。

qxsslcnc

qxsslcnc1#

试试这个,它使用一个生成器来排除not_ok_name列表中的名字。

import csv
with open("C:/path/a.csv","rU") as f,open("C:/path/des.csv","wb") as w:
    not_ok_name= ["John Doe" , "Jim Foe"]
    reader = csv.reader(f)
    for row in reader:
        name = row[0]
        if name not in not_ok_name:
            w.write(row)
col17t5w

col17t5w2#

not_ok_name = ["John", "Jim"]
not_ok_name = set(not_ok_name)  # sets give us O(1) lookup times

with open('myfile.csv') as infile, open('corrected.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    for name, url in csv.reader(infile):  # for each row in the input file
        fname = name.split(None, 1)[0]
        if fname in not_ok_name:
            continue  # if the first name is in the list, ignore the row
        writer.writerow([name, url])

相关问题