我的数据如下
[['15/09/16, 12:21 pm - User1: Hey'],
['15/09/16, 12:22 pm - User2: <Media omitted>'],
["15/09/16, 12:22 pm - User2: It's yesterday's work"],
['15/09/16, 12:22 pm - User1: Gotta work on it.']]
我正在尝试将此嵌套列表分为日期,时间,用户名和消息的每一列。
现在我的分隔符是
,
以分隔日期,
-
以分隔时间,
:
分隔用户名和消息
但是问题是,如果我使用:
,它也会同时分隔时间,因为它的格式为XX:XX
。
到目前为止,我的第一步是正确地进行分割,然后我可以继续转换为csv。
尝试1- 我尝试在读取时直接拆分数据,但是没有任何变化。
delim=",","-",":"
regexPattern = '|'.join(map(re.escape, delim))
data = []
for line in open('/content/drive/My Drive/sample.txt'):
items = line.rstrip('\r\n').split(regexPattern) # strip new-line characters and split on column delimiter
items = [item.strip() for item in items] # strip extra whitespace off data items
data.append(items)
尝试2- 我尝试在写入csv时拆分
delim=",","-",":"
regexPattern = '|'.join(map(re.escape, delim))
with open('output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
re.split(regexPattern,data)
writer.writerows(data)
这会出错,因为split需要一个字符串,并且我有一个列表。不确定如何实现我的主要目标。
感谢您的帮助。
答案 0 :(得分:3)
使用模式handleerror
例如:
re.compile(r",|\-|\:\s+")
输出:
import re
data = [['15/09/16, 12:21 pm - User1: Hey'],
['15/09/16, 12:22 pm - User2: <Media omitted>'],
["15/09/16, 12:22 pm - User2: It's yesterday's work"],
['15/09/16, 12:22 pm - User1: Gotta work on it.']]
regexPattern = re.compile(r",|\-|\:\s+")
for i in data:
for j in i:
print(regexPattern.split(j))
使用正则表达式分组。
演示:
['15/09/16', ' 12:21 pm ', ' User1', 'Hey']
['15/09/16', ' 12:22 pm ', ' User2', '<Media omitted>']
['15/09/16', ' 12:22 pm ', ' User2', "It's yesterday's work"]
['15/09/16', ' 12:22 pm ', ' User1', 'Gotta work on it.']
输出:
import re
data = [['15/09/16, 12:21 pm - User1: Hey'],
['15/09/16, 12:22 pm - User2: <Media omitted>'],
["15/09/16, 12:22 pm - User2: It's yesterday's work"],
['15/09/16, 12:22 pm - User1: Gotta work on it, what,hello.']]
regexPattern = re.compile(r"(?P<date>\d{2,}\/\d{2,}\/\d{2,}),\s*(?P<time>\d{2,}:\d{2,}\s*[a-z]{2,})\s*\-\s*(?P<user>\w+)\:\s*(?P<msg>.*)$")
for i in data:
for j in i:
print(regexPattern.match(j).groups())
答案 1 :(得分:2)
没有RegEx
def parse(item):
date_time, user_message = item.split(' - ', 1)
return [*date_time.split(', '), *user_message.split(': ', 1)]
eggs = [['15/09/16, 12:21 pm - User1: Hey'],
['15/09/16, 12:22 pm - User2: <Media omitted>'],
["15/09/16, 12:22 pm - User2: It's yesterday's work"],
['15/09/16, 12:22 pm - User1: Gotta work on it.']]
spam = [parse(egg[0]) for egg in eggs]
print(spam)
输出
[['15/09/16', '12:21 pm', 'User1', 'Hey'],
['15/09/16', '12:22 pm', 'User2', '<Media omitted>'],
['15/09/16', '12:22 pm', 'User2', "It's yesterday's work"],
['15/09/16', '12:22 pm', 'User1', 'Gotta work on it.']]
答案 2 :(得分:1)
这是使用正则表达式组的完美案例。
s = '15/09/16, 12:21 pm - User1: Hey'
ms = re.match(r'(\d+/\d+/\d+).+?(\d+:\d+).+-\s(.*):\s(.*)', s)
print(ms.groups()) # ('15/09/16', '12:21', 'User1', 'Hey')
您可以将它们重新加入csv行。