如何将此.txt转换为数据框?

时间:2018-08-21 21:31:09

标签: python pandas parsing whatsapp

我正在尝试在Python中进行Whatsapp分析,我想将其转换为包含日期,小时,人和消息列的数据框。

 '[8/23/17, 1:45:10 AM] Guillermina: Guten Morgen',
 '[8/23/17, 1:47:05 AM] Kester Stieldorf: Good morning :) was in Düsseldorf one hour ago ;)',
 '[8/23/17, 1:47:16 AM] Guillermina: Hahahaha',
 '[8/23/17, 1:47:19 AM] Guillermina: What?',
 '[8/23/17, 1:47:36 AM] Kester Stieldorf: Yeah had to pick something up',

文本比那更长。我已经尝试过:

pieces = [x.strip('\n') for x in file_read.split('\n')]
beg_pattern = r'\d+/\d+/\d+,\s+\d+:\d+\s+\w+\.\w+\.'
pattern = r'\d+/(\d+/\d+),\s+\d+:\d+\s+\w+\.\w+\.\s+-\s+(\w+|\w+\s+\w+|\w+\s+\w+\s+\w+|\w+\s+\w+\.\s+\w+|\w+\s+\w+-\w+|\w+\'\w+\s+\w+|\+\d+\s+\(\W+\d+\)\s+\d+-\d+\W+|\W+\+\d+\s+\d+\s+\d+\s+\d+\W+|\W+\+\d+\s+\d+\w+\W+):(.*)'

reg = re.compile(beg_pattern)
regex = re.compile(pattern)

remove_blanks = [x for x in pieces if reg.match(x)]
blanks = [x for x in pieces if not reg.match(x)]

grouped_data = []
for x in remove_blanks:
    grouped_data.extend(regex.findall(x))

grouped_data_list = [list(x) for x in grouped_data] 

但是它看起来没有用。我很确定re.compile()有问题,因为当我打印reg和regex时,它们返回空数组。我该怎么解决?

1 个答案:

答案 0 :(得分:1)

首先,解析您的文件:

with open('file.txt') as f:
    pieces = [i.strip() for i in f.read().splitlines()]

然后使用re.findall

pd.DataFrame(
    re.findall(r'\[(.*?)\]\s*([^:]+):\s*(.*)', '\n'.join(pieces)),
    columns=['Time', 'Name', 'Text']
)

                  Time              Name  \
0  8/23/17, 1:45:10 AM       Guillermina
1  8/23/17, 1:47:05 AM  Kester Stieldorf
2  8/23/17, 1:47:16 AM       Guillermina
3  8/23/17, 1:47:19 AM       Guillermina
4  8/23/17, 1:47:36 AM  Kester Stieldorf

                                                Text
0                                       Guten Morgen
1  Good morning :) was in Düsseldorf one hour ago ;)
2                                           Hahahaha
3                                              What?
4                      Yeah had to pick something up