Python 我想使用';关键词';帮助我筛选csv数据

Python 我想使用';关键词';帮助我筛选csv数据,python,csv,filter,Python,Csv,Filter,我必须从给定的信息中过滤出csv数据并打印出来。我是python新手,我脑子里想的是,我会在列表中创建关键字,然后我会编写一些函数,如果csv数据中有这些单词,它会打印出我的数据 这就是我试过的 keywords = ("AIG","Baylor","CGG","Citi","EcconMobil","Flow-Cal Inc.", "Global SHop Solutions","Harris Count CTS","HCSS", "Hitachi Consulti

我必须从给定的信息中过滤出csv数据并打印出来。我是python新手,我脑子里想的是,我会在列表中创建关键字,然后我会编写一些函数,如果csv数据中有这些单词,它会打印出我的数据

这就是我试过的

keywords = ("AIG","Baylor","CGG","Citi","EcconMobil","Flow-Cal Inc.",
       "Global SHop Solutions","Harris Count CTS","HCSS",
       "Hitachi Consulting", "HP Inc.","INT Inc.","JPMorgan Chase & Co",
       "Leidos","McKesson","MRE Consulting Ltd.","NetIQ","PROS",
       "San Jacinto College","SAS","Smartbridge","Sogeti USA",
       "Southwest Research Institute","The Reynolds and Reynolds Company",
       "UH Enterprise Systems","U.S. Marine Corps","ValuD Consuting LLC","Wipro")

pattern = (" ".join(keywords))
with f as filterf:
    for line in filterf:
        if not re.search(pattern,line):
            print(line.strip())

print(pattern)
这是我需要打印出来的

0 AIG,10,,,,Yes,,,Jr,,,MS,,
1 Baylor College of Medicine,19,Yes,Yes,,,,,,,,,,Recent
2 CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
3 Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
4 ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
5 Flow-Cal Inc.,16,Yes,,,Yes,,,Jr,Sr,,,,All
6 Global Shop Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
7 Harris County CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
8 HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
9 Hitachi Consulting,13,Yes,,,,,,,Sr,,MS,,
10 HP Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
11 INT Inc.,20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
12 JPMorgan Chase & Co,3,Yes,,,Yes,,,Jr,Sr,,,,
13 Leidos,390,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
14 McKesson,26,Yes,,,,,,,Sr,,,,
15 MRE Consulting Ltd.,2,Yes,,,,,,,Sr,PB,MS,,All
16 NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,,
17 PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
18 San Jacinto College ,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
19 SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
20 Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
21 Sogeti USA,15,Yes,,,,,,,Sr,PB,MS,,
22 Southwest Research Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
23 The Reynolds and Reynolds Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
24 UH Enterprise Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
25 U.S. Marine Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
26 ValuD Consuting LLC,5,Yes,,,,,,,Sr,PB,,,All
27 Wipro,24,Yes,,,,,,,Sr,PB,,,
这是我的csv数据

>   ALPHABETICAL ORDER,,,,,,,,,,,,,
> ,,Positions,,,,Classifications,,,,,,,
> Company,Booth,Full-Time,"Full-Time Visa
> Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
> AIG,10,,,,Yes,,,Jr,,,MS,,
> Baylor�College�of�Medicine,19,Yes,Yes,,,,,,,,,,Recent
> CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
> ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,, ,...
> Flow-Cal�Inc.,16,Yes,,,Yes,,,Jr,Sr,,,,All
> Global�Shop�Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
> Harris�County�CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
> HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
> Hitachi�Consulting,13,Yes,,,,,,,Sr,,MS,,
> HP�Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
> INT�Inc.,20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
> JPMorgan�Chase�&�Co,3,Yes,,,Yes,,,Jr,Sr,,,,
> Leidos,390,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
> McKesson,26,Yes,,,,,,,Sr,,,, ,,,,,,,,,,,,,
> MRE�Consulting�Ltd.,2,Yes,,,,,,,Sr,PB,MS,,All
> NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,, PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
> San�Jacinto�College��,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
> SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
> Smartbridge,8,Yes,,,,,,,Sr,PB,MS,, Sogeti�USA,15,Yes,,,,,,,Sr,PB,MS,,
> Southwest�Research�Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
> The�Reynolds�and�Reynolds�Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
> UH�Enterprise�Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
> U.S.�Marine�Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
> ValuD�Consuting�LLC,5,Yes,,,,,,,Sr,PB,,,All
> Wipro,24,Yes,,,,,,,Sr,PB,,, BOOTH ORDER,,,,,,,,,,,,,
> ,Booth,Positions,,,,Classifications,,,,,,,
> Company,#,Full-Time,"Full-Time Visa
> Sponsor",Part-Time,Internship,Freshman,Sophomore,Junior,Senior,Post-Bacs,MS,PhD,Alumni
> HP�Inc.,1,Yes,,,Yes,,,Jr,,,MS,,Recent
> "MRE�Consulting,�Ltd.",2,Yes,,,,,,,Sr,PB,MS,,All
> JPMorgan�Chase�&�Co,3,Yes,,,Yes,,,Jr,Sr,,,,
> SAS,4,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
> ValuD�Consuting�LLC,5,Yes,,,,,,,Sr,PB,,,All
> NetIQ,7,,,,Yes,,Soph,Jr,Sr,PB,,, Smartbridge,8,Yes,,,,,,,Sr,PB,MS,,
> UH�Enterprise�Systems,9,Yes,Yes,Yes,Yes,Fr,Soph,Jr,Sr,PB,MS,PhD,All
> AIG,10,,,,Yes,,,Jr,,,MS,, ExxonMobil,11,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,,,
> Southwest�Research�Institute,12,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
> Hitachi�Consulting,13,Yes,,,,,,,Sr,,MS,,
> San�Jacinto�College��,14,,,,Yes,,Soph,Jr,Sr,PB,MS,,
> Sogeti�USA,15,Yes,,,,,,,Sr,PB,MS,,
> "Flow-Cal,�Inc.",16,Yes,,,Yes,,,Jr,Sr,,,,All
> CGG,17,Yes,Yes,,,,,,,,MS,PhD,Recent
> Global�Shop�Solutions,18,Yes,,,Yes,,,,Sr,PB,,,All
> Baylor�College�of�Medicine,19,Yes,Yes,,,,,,,,,,Recent
> "INT,�Inc.",20,Yes,Yes,,Yes,,,Jr,Sr,,MS,PhD,
> PROS,21,Yes,,,,,,,Sr,,MS,PhD,All
> Harris�County�CTS,22,Yes,,,Yes,,,Jr,Sr,PB,MS,PhD,All
> The�Reynolds�and�Reynolds�Company,23,Yes,Yes,,Yes,Fr,Soph,Jr,Sr,PB,,,All
> Wipro,24,Yes,,,,,,,Sr,PB,,,
> U.S.�Marine�Corps,25,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,All
> McKesson,26,Yes,,,,,,,Sr,,,, Citi,27/28,Yes,,,Yes,,,Jr,Sr,,,,
> HCSS,29,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,Recent
> Leidos,30,Yes,,,Yes,Fr,Soph,Jr,Sr,PB,MS,,
我一直在网上寻找,花了很多时间阅读东西。我是全新的,它是如此的强大。任何提示或想法都将不胜感激

这是我的指纹

AIG Baylor CGG Citi EcconMobil Flow-Cal Inc. Global SHop Solutions Harris Count CTS HCSS Hitachi Consulting HP Inc. INT Inc. JPMorgan Chase & Co Leidos McKesson MRE Consulting Ltd. NetIQ PROS San Jacinto College SAS Smartbridge Sogeti USA Southwest Research Institute The Reynolds and Reynolds Company UH Enterprise Systems U.S. Marine Corps ValuD Consuting LLC Wipro

您只是在打印您的模式值。搜索和打印没有打印任何内容。试试这个

with f as filterf:
    output_line_counter = 0
    for line in filterf:
        if any(keyword in line for keyword in keywords):
            print(output_line_counter, line.strip())
            output_line_counter += 1

删除此之后的
打印(模式)

当前代码是如何失败的?将输入减少到应匹配的单行、不应匹配的单行和两个模式(一个匹配一行,一个不匹配一行)。试着一次从零到一个完整的解决方案会让人沮丧。嗨!它没有失败只是没有像这样打印“AIG贝勒CGG花旗EcconMobil Flow Cal Inc.全球商店解决方案Harris Count CTS HCSS Hitachi Consulting HP Inc.INT Inc.JP Morgan Chase&Co Leidos McKesson MRE Consulting Ltd.NetIQ PROS San Jacino College SAS Smartbridge Sogeti USA西南研究所雷诺和雷诺公司UH Enterprise Systems美国海军陆战队ValuD Consuting LLC Wipro’您的要求是,您只希望打印出包含其中一个关键字的行,而跳过其他行?它会打印出来,因为您在最后打印图案。这就是你们的模式。哇,你们真是帮了大忙,非常感谢。现在来看看如何删除它们之间的空格。非常感谢你!!!你的意思是两行文字之间有一个空行?如果是这样,请删除
print()!谢谢:)我终于可以进入下一部分了。我唯一想知道的是,关键字的剩余数字没有显示出来。从我发布的第二个文本块中可以看到,它显示了左侧的数字,因为我猜这些是cvs文件中的行。有什么输入吗?你必须手动添加行号。检查修改后的答案