In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use regular expressions in python to get the date and time in a string. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Processing before extraction date
1. Unifying the date format for dealing with text data
Text = "on August 31, 2015, an object attack accident occurred in the finished product warehouse of Quzhou Yuanli Metal products Co., Ltd. (hereinafter referred to as Yuanli Storage Company), resulting in a direct economic loss of 950000 yuan." Text1 = "Administrative penalty decision issued on 2015-12-28" text2 = "an object strike accident occurred in August 2015" # deal with the text # 2015-8-31 2015-12-28text = text.replace ("year", "-"). Replace ("month", "-"). Replace ("day", "). Replace (" / ","-"). Strip ()
two。 Regular expression for extracting time
# Oct 27th 2019 9:46:21 "(\ d {4} -\ d {1recov 2} -\ d {1rem 2}:\ d {1pr 2}:\ d {1pr 2}:\ d {1pr 2})" # Oct 27th 2019 "(\ d {4} -\ d {1pm 2} -\ d {1cr 2})" # Oct 2019 "(\ d {4} -\ d {1j 2})" # Oct 2019 "(\ d {4} -\ d {1j 2})"
3. Encapsulate it
Def get_strtime (text): text = text.replace ("year", "-"). Replace ("month", "-"). Replace ("day", "). Replace (" / ","-"). Strip () text = re.sub ("\ s + "," Text) t = "regex_list = [# # 22:46:21 on August 15, 2013" (\ d {4} -\ d {1meme2} -\ d {1meme2}:\ d {1Power2}:\ d {1Power2}:\ d {1flint 2}), # "22:46 on August 15, 2013" (\ d {4} -\ d {1cr 2} -\ d {1cr 2}:\ d {1pm 2}) " # for regex in regex_list: t = re.search (regex, text) if t: t = t.group (1) return t else: print ("effective date not obtained")
Ps: let's take a look at the date in the python extraction string
Import re# deletes the Chinese characters def subChar (str): match=re.compile (u' [\ u4e00 -\ u9fa5]') return match.sub (', str) # extraction date def extractDate (str): if not str: return None raw=subChar (str) if not raw: return None # extract the first 10 characters rawdate=raw [: 10] datelist=re.findall ("\ d +") Rawdate) if not datelist: return None if datelist.__len__ () = = 3: if (float (datelist [0]) > 2099 or float (datelist [0]) 12 or float (datelist [2]) > 31: return None else: return'- '.join (datelist) if datelist.__len__ () = = 2: if (float (datelist [0]) > 2099 or float (datelist [0]) 12: return None else: Datelist.append ('01') return'-'.join (datelist) if datelist.__len__ () = = 1: if float (datelist [0]) > 20991231 or float (datelist [0])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.