Python3仅从csv文件中提取电子邮件

问题描述 投票:-1回答:1

我编写了一个工作脚本,该脚本从.csv文件中提取信息。但是,提取后,当我编写代码专门查找@符号时,它会打印出所有信息,而不是电子邮件。

 #!/bin/python3
    import re
    def print_csv():
            in_file = open('sample._data.csv', 'rt')
            for line in in_file:
                    if re.findall(r'(.*)@(.*).(.*)', line):
                            print(line)
    print_csv()

这里是输出示例:

"Carlee","Boulter","Tippett, Troy M Ii","8284 Hart St","Abilene","Dickinson","KS",67410,"785-347-1805","785-253-7049","[email protected]","http://www.tippetttroymii.com"

"Thaddeus","Ankeny","Atc Contracting","5 Washington St #1","Roseville","Placer","CA",95678,"916-920-3571","916-459-2433","[email protected]","http://www.atccontracting.com"

"Jovita","Oles","Pagano, Philip G Esq","8 S Haven St","Daytona Beach","Volusia","FL",32114,"386-248-4118","386-208-6976","[email protected]","http://www.paganophilipgesq.com"

"Alesia","Hixenbaugh","Kwikprint","9 Front St","Washington","District of Columbia","DC",20001,"202-646-7516","202-276-6826","[email protected]","http://www.kwikprint.com"

"Lai","Harabedian","Buergi & Madden Scale","1933 Packer Ave #2","Novato","Marin","CA",94945,"415-423-3294","415-926-6089","[email protected]","http://www.buergimaddenscale.com"

"Brittni","Gillaspie","Inner Label","67 Rv Cent","Boise","Ada","ID",83709,"208-709-1235","208-206-9848","[email protected]","http://www.innerlabel.com"

"Raylene","Kampa","Hermar Inc","2 Sw Nyberg Rd","Elkhart","Elkhart","IN",46514,"574-499-1454","574-330-1884","[email protected]","http://www.hermarinc.com"

"Flo","Bookamer","Simonton Howe & Schneider Pc","89992 E 15th St","Alliance","Box Butte","NE",69301,"308-726-2182","308-250-6987","[email protected]","http://www.simontonhoweschneiderpc.com"

"Jani","Biddy","Warehouse Office & Paper Prod","61556 W 20th Ave","Seattle","King","WA",98104,"206-711-6498","206-395-6284","[email protected]","http://www.warehouseofficepaperprod.com"

"Chauncey","Motley","Affiliated With Travelodge","63 E Aurora Dr","Orlando","Orange","FL",32804,"407-413-4842","407-557-8857","[email protected]","http://www.affiliatedwithtravelodge.com"

我正在尝试做的是使输出看起来像电子邮件列表。我无法从csv文件中过滤掉其他内容。

python-3.x csv extract
1个回答
0
投票

如前所述,您应该可以使用内置的csv库。如果文件是csv,则它应具有结构化格式,即使它没有列名,也应该能够按列位置提取它。根据您的样本数据,您可以按位置获取正确的列。请检查official Python docs

>>> import os
>>> import csv
>>> with open('sample._data.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=',',quotechar='"')
    for row in reader:
        print(row[10])

    # output:   
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
© www.soinside.com 2019 - 2024. All rights reserved.