简体   繁体   English

使用正则表达式读取Python中的文本字段

[英]read a text field in Python using regular expressions

I have text file, like 我有文本文件,例如

FILED AS OF DATE:       20090209
DATE AS OF CHANGE:      20090209

I need to find the position using FILED AS OF DATE: and read the date. 我需要使用FILED AS OF DATE:查找职位FILED AS OF DATE:并阅读日期。 I know how to do it using python strings. 我知道如何使用python字符串做到这一点。 But using a regular expression seems cooler:) 但是使用正则表达式似乎更酷:)

Btw, how to parse the date? 顺便说一句,如何解析日期?

Thanks! 谢谢!

#!/usr/bin/env python
import datetime, fileinput, re

for line in fileinput.input():
    if 'FILED AS OF DATE' in line:
       line = line.rstrip()
       dt = datetime.datetime.strptime(line, 'FILED AS OF DATE:       %Y%m%d')

       # or with regex
       date_str, = re.findall(r'\d+', line)
       dt = datetime.datetime.strptime(date_str, '%Y%m%d')

       print dt.date()

Example: 例:

$ ./finddate.py input.txt

Output: 输出:

2009-02-09

Is this what you need? 这是您需要的吗?

/FILED.*([0-9]{4})([0-9]{2})([0-9]{2})$/

Search for FILED then anything then parses date divided in 3 groups. 然后搜索FILED,然后将所有内容解析为3组。

You really do not need to use RE for this. 您确实不需要为此使用RE

Regarding parsing date, you can use datetime.strptime(date_string, format) . 关于解析日期,您可以使用datetime.strptime(date_string,format) Then you can convert it from datetime.datetime to datetime.date if required. 然后,可以根据需要将其从datetime.datetime转换为datetime.date

Alternatively use python-dateutil parse() function , which is quite handy when the format of your date(time) value is not fixed. 或者使用python-dateutil parse()函数 ,当您的date(time)值的格式不固定时,这非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM