简体   繁体   English

如何仅从 python 中的文本文件中获取日期

[英]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.

相关问题 如何使用python在约会后获取文本? - How do I get the text after a date using python? 如何使用 python 仅将 URL 中的特定文本行保存到 txt 文件? - How do I save only specific lines of text from a URL to a txt file using python? Python-如何仅输入输入文本文件中的case语句和实例以$开头? - Python - How do i write only case statements and instances start with $ from input text file? 如何从日期对象python获取日期时间? - How do I get datetime from date object python? 我如何只得到时间,没有日期? - how Do i get the time only, no date? 如何在python中仅从具有日期时间和文本值的字段中获取日期和文本值? - How to get only date and text values from a field with Datetime and text values in python? 如何让 python 解释从文本文件读取的字符串中 colors 的 ANSI 转义码 - How do I get python to interpret the ANSI escape codes for colors in a string read from a text file 如何使用 input() 从文本文件中获取输入数据的 python 的代码? - How do I make the code of python which get the input data from a text file with using input()? 如何仅从python词典中获取一个单词? - How do I only get one word from a dictionary in python? 如何从 python 的输入框中获取文本? - How do I get the text from an entry box in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM