首页 > 解决方案 > 嗨,试图从文本文件中提取名称 + 电子邮件,但不知道如何将输出彼此相邻

问题描述

我试图让我的数据并排输出,姓名和电子邮件。现在它只打印电子邮件,然后打印姓名。

这是我的代码:

import re
import nltk
from nltk.corpus import stopwords
stop = stopwords.words('english')

inputfile = open('/Users/jchome/Downloads/StockXRF/untitled.txt','r')
string = inputfile.read()



def extract_email_addresses(string):
    r = re.compile(r'[\w\.-]+@[\w\.-]+')
    return r.findall(string)

def ie_preprocess(document):
    document = ' '.join([i for i in document.split() if i not in stop])
    sentences = nltk.sent_tokenize(document)
    sentences = [nltk.word_tokenize(sent) for sent in sentences]
    sentences = [nltk.pos_tag(sent) for sent in sentences]
    return sentences

def extract_names(document):
    names = []
    sentences = ie_preprocess(document)
    for tagged_sentence in sentences:
        for chunk in nltk.ne_chunk(tagged_sentence):
            if type(chunk) == nltk.tree.Tree:
                if chunk.label() == 'PERSON':
                    names.append(' '.join([c[0] for c in chunk]))
    return names

if __name__ == '__main__':
    emails = extract_email_addresses(string)
    names = extract_names(string)


print (emails + names)

输出:

['1lawrencenage1l@gmail.com', george@gmail.com, 'Lawrence', 'George']

如何将输出彼此相邻并写入文本文件?

标签: pythonextraction

解决方案


您可以执行以下操作:

import pandas as pd
zipped = list(zip(emails, names))
df = pd.DataFrame(zipped, columns = ['emails' , 'names'])

在此之后,您可以打印数据框,并且可以使用例如to_csv方法将输出保存到文件中。


推荐阅读