[英]How do I get only date from text file in python
I have a very big text file and I'm reading it in python.我有一个非常大的文本文件,我正在 python 中阅读它。 I have opened the file in read mode, got data in a variable.
我以读取模式打开文件,在变量中获取数据。 Now I want only date from it.
现在我只想从中约会。 So I read using readline() function and applied for loop and split the each line result by comma and getting result of index[0].
所以我使用 readline() function 阅读并申请循环并用逗号分割每行结果并获得索引[0]的结果。 So that I get a list of dates.
这样我就得到了日期列表。 But in text file some of the section is like shown below.
但在文本文件中,某些部分如下所示。 Because of this I'm getting 'And bitNumber is 4', 'Then function si', 'Take a char variable' also in my output.
因此,在我的 output 中,我也得到了“并且 bitNumber 为 4”、“然后 function si”、“使用 char 变量”。
10/04/2020, 03:05 - ABC: Like if number is 0011 0111
And bitNumber is 4
Then function si
10/04/2020, 03:08 - ABC: Question 6
Take a char variable, apply a same as number
10/04/2020, 03:08 - ABC: Example If my variable is 0X3C answer should be same
What I do to avoid getting 'And bitNumber is 4', 'Then function si', 'Take a char variable' this in output and should only get dates我做些什么来避免在 output 中得到“并且 bitNumber 是 4”、“然后 function si”、“使用 char 变量”并且应该只获取日期
for row_data in data_collected:
print(row_data.split(',')[0])
Pass each possible date to datetime.strptime
.将每个可能的日期传递给
datetime.strptime
。 If it doesn't look like a date this will raise a ValueError
.如果它看起来不像日期,则会引发
ValueError
。 Assuming all your dates are formatted the same:假设您所有日期的格式都相同:
from datetime import datetime
dates = []
for row in data:
date = row.split(',', 1)[0]
try:
date = datetime.strptime(date, '%m/%d/%Y')
dates.append(date)
except ValueError:
continue
Bonus: now you have datetime.datetime
objects instead of just strings.奖励:现在您有了
datetime.datetime
对象,而不仅仅是字符串。
You can look for dates in that format with regex:您可以使用正则表达式查找该格式的日期:
import re
....
for row_data in data_collected:
if re.match(r'\d\d/\d\d/\d\d\d\d',row_data):
print(row_data.split(',')[0])
that will catch dates in the form nn/nn/nnnn (the \d in a regex means to match any digit)这将以 nn/nn/nnnn 形式捕获日期(正则表达式中的 \d 表示匹配任何数字)
You can use Regular Expression to extract data as below您可以使用正则表达式来提取数据,如下所示
import re
dates = []
with open('sample.txt','r') as f:
for l in f.readlines():
match = re.search(r'\d{2}/\d{2}/\d{4}', l)
if match is not None:
dates.append(match.group())
This is the most flexible way and it will work on any delimiter.这是最灵活的方式,它适用于任何分隔符。
Your regex "(?P<day>0[1-9]|[12][0-9]|3[01])(?P<delimiter>[- /.])(?P<month>0[1-9]|1[012])\2(?P<year>(?:19|20)\d\d)"
let say your data is in string "X"
你的正则表达式
"(?P<day>0[1-9]|[12][0-9]|3[01])(?P<delimiter>[- /.])(?P<month>0[1-9]|1[012])\2(?P<year>(?:19|20)\d\d)"
假设您的数据在字符串"X"
中
we will do so.我们会这样做。
import re
result_list = re.findall("(?P<day>0[1-9]|[12][0-9]|3[01])(?P<delimiter>[- /.])(?P<month>0[1-9]|1[012])\2(?P<year>(?:19|20)\d\d)", x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.