gpt4 book ai didi

python - 将文本格式的数据读入 Python Pandas 数据框

转载 作者:行者123 更新时间:2023-11-28 17:29:57 25 4
gpt4 key购买 nike

我在 Windows 上运行 Python 2.7。

我有一个大文本文件 (2 GB),涉及 50 万多封电子邮件。该文件没有明确的文件类型,格式为:

email_message#: 1
email_message_sent: 10/10/1991 02:31:01
From: tomf@abc.com| Tom Foo |abc company|
To: adee@abc.com| Alex Dee |abc company|
To: benfor12@xyz.com| Ben For |xyz company|
email_message#: 2
email_message_sent: 10/12/1991 01:28:12
From: timt@abc.com| Tim Tee |abc company|
To: tomf@abc.com| Tom Foo |abc company|
To: adee@abc.com| Alex Dee |abc company|
To: benfor12@xyz.com| Ben For|xyz company|
email_message#: 3
email_message_sent: 10/13/1991 12:01:16
From: benfor12@xyz.com| Ben For |xyz company|
To: tomfoo@abc.com| Tom Foo |abc company|
To: t212@123.com| Tatiana Xocarsky |numbers firm |
...

如您所见,每封电子邮件都有以下相关数据:

1) 发送时间

2) 发送邮件的邮箱

3)发件人姓名

4) 该人工作的公司

5) 收到电子邮件的每个电子邮件地址

6) 每个收到邮件的人的名字

7) 每个收到邮件的人的公司

在文本文件中有 500K+ 封电子邮件,电子邮件最多可以有 16K 收件人。在电子邮件中如何引用人名或他们工作的公司名称方面没有任何模式。

我想获取这个大文件并在 python 中对其进行操作,使其最终成为 Pandas Dataframe。我想要 pandas dataframe 格式如下 excel 的截图:

Sample Data Structure

编辑

我解决这个问题的计划是编写一个“解析器”,它接受这个文本文件并读取每一行,将每行中的文本分配给 pandas dataframe 的特定列

我打算写类似下面的东西。有人可以确认这是执行此操作的正确方法吗?我想确保我没有丢失内置的 pandas 函数或来自不同 module 的函数。

#connect to object 
data = open('.../Emails', 'r')

#build empty dataframe
import pandas as pd
df = pd.DataFrame()

#function to read lines of the object and put pieces of text into the
# correct column of the dataframe
for line in data:
n = data.readline()
if n.startswith("email_message#:"):
#put a slice of the text into a dataframe
elif n.startswith("email_message_sent:"):
#put a slice of the text into a dataframe
elif n.startswith("From:"):
#put slices of the text into a dataframe
elif n.startswith("To:"):
#put slices of the text into a dataframe

最佳答案

我无法抗拒这种痒,所以这是我的方法。

from __future__ import unicode_literals

import io

import pandas as pd
from pandas.compat import string_types


def iter_fields(buf):
for l in buf:
yield l.rstrip('\n\r').split(':', 1)


def iter_messages(buf):
it = iter_fields(buf)
k, v = next(it)
while True:
n = int(v)
_, v = next(it)
date = pd.Timestamp(v)
_, v = next(it)
from_add, from_name, from_comp = v.split('|')[:-1]
k, v = next(it)
to = []
while k == 'To':
to_add, to_name, to_comp = v.split('|')[:-1]
yield (n, date, from_add[1:], from_name[1:-1], from_comp,
to_add[1:], to_name[1:-1], to_comp)
k, v = next(it)

if not hasattr(filepath_or_buffer, read):
filepath_or_buffer


def _read_email_headers(buf):
columns=['email_message#', 'email_message_sent',
'from_address', 'from_name', 'from_company',
'to_address', 'to_name', 'to_company']
return pd.DataFrame(iter_messages(buf), columns=columns)


def read_email_headers(path_or_buf):
close_buf = False
if isinstance(path_or_buf, string_types):
path_or_buf = io.open(path_or_buf)
close_buf = True
try:
return _read_email_headers(path_or_buf)
finally:
if close_buf:
path_or_buf.close

这是你将如何使用它:

df = read_email_headers('.../data_file')

只需使用您的文件路径调用它,您就拥有了数据框。

现在,以下内容仅供测试之用。在现实生活中,您不会这样做来处理您的实际数据。

由于我(或随机的 StackOverflow 读者)没有您的文件副本,我必须使用字符串来伪造它:

text = '''email_message#: 1
email_message_sent: 10/10/1991 02:31:01
From: tomf@abc.com| Tom Foo |abc company|
To: adee@abc.com| Alex Dee |abc company|
To: benfor12@xyz.com| Ben For |xyz company|
email_message#: 2
email_message_sent: 10/12/1991 01:28:12
From: timt@abc.com| Tim Tee |abc company|
To: tomf@abc.com| Tom Foo |abc company|
To: adee@abc.com| Alex Dee |abc company|
To: benfor12@xyz.com| Ben For|xyz company|'''

然后我可以创建一个类似文件的对象并将其传递给函数:

df = read_email_headers(io.StringIO(text))
print(df.to_string())

email_message# email_message_sent from_address from_name from_company to_address to_name to_company
0 1 1991-10-10 02:31:01 tomf@abc.com Tom Foo abc company adee@abc.com Alex Dee abc company
1 1 1991-10-10 02:31:01 tomf@abc.com Tom Foo abc company benfor12@xyz.com Ben For xyz company
2 2 1991-10-12 01:28:12 timt@abc.com Tim Tee abc company tomf@abc.com Tom Foo abc company
3 2 1991-10-12 01:28:12 timt@abc.com Tim Tee abc company adee@abc.com Alex Dee abc company
4 2 1991-10-12 01:28:12 timt@abc.com Tim Tee abc company benfor12@xyz.com Ben Fo xyz company

或者,如果我想使用实际文件:

with io.open('test_file.txt', 'w') as f:
f.write(text)

df = read_email_headers('test_file.txt')
print(df.to_string()) # Same output as before.

但是,再次声明,您不必执行此操作即可将函数用于您的数据。只需用文件路径调用它即可。

关于python - 将文本格式的数据读入 Python Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35158954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com