嘿伙计我通常不使用正则表达式,所以我需要一些帮助来从下面的字符串中获取一些匹配。我只希望粗体信息与正则表达式匹配,任何帮助或解释都将不胜感激。
'" FM 2222 RD / RIVER PLACE BLVD"," 0:" ,," 18:"," 00& #34;," 2008-08-14 "," CRASH /离开场景"," 30.39452568""(30.39452568-97.84551164)"" -97.84551164"" 18:00:00"" 4&# 34;," 2008-08-14 18:00:00-06:00"," 20085043619"'
答案 0 :(得分:2)
如果您需要检测日期和旁边的字段,可以使用以下正则表达式:
expression = '"\d{4}-\d{2}-\d{2}","[^"]*"'
工作示例:
import re
my_str = '"FM 2222 RD / RIVER PLACE BLVD","0:",,"18:","00","2008-08-14","CRASH/LEAVING THE SCENE","30.39452568","(30.39452568-97.84551164)","-97.84551164","18:00:00","4","2008-08-14 18:00:00-06:00","20085043619"'
expression = '"\d{4}-\d{2}-\d{2}","[^"]*"'
re.findall(expression, my_str) # returns ['"2008-08-14","CRASH/LEAVING THE SCENE"']
答案 1 :(得分:0)
您提供的字符串看起来像csv文件中的一行,另一个选项是使用Pythons csv module。
由于我不知道你是否有一个充满这些字符串的文件或列表,这个例子展示了如何使用csv.reader
将这个字符串读入io.StringIO
(Python 3.6)。 4)
In[2]: s = '"FM 2222 RD / RIVER PLACE BLVD","0:",,"18:","00","2008-08-14","CRASH/LEAVING THE SCENE","30.39452568","(30.39452568-97.84551164)","-97.84551164","18:00:00","4","2008-08-14 18:00:00-06:00","20085043619"'
...:
In[3]: import csv
...: import io
...:
...: reader = csv.reader(io.StringIO(s))
...: for line in reader:
...: address, a, b, c, d, date, msg, *stuff = line
...: print(date)
...: print(msg)
...:
2008-08-14
CRASH/LEAVING THE SCENE
如果您的实际csv文件包含标题,则可以使用csv.DictReader
然后执行print(line['date'])
之类的操作(假设密钥为'date')。