简体   繁体   English

Python-如果没有日期模式,则跳过

[英]Python - skip if has not pattern of date

I have a csv where I have for example this: 我有一个csv,例如:

Date,Comment

2014-05-29,Last time we will see

What about next time?"

2014-05-29,"""still want to be seen as the good guys..."""

This is my world. 

2014-05-29,And so the game begins... ;)

2014-05-29,"Btw, this is... 

And I would like to skip those rows which have not a date format in first column. 我想跳过第一列中没有日期格式的那些行。 I have this: 我有这个:

a = []
csvReader = csv.reader(open(csv_file_to_open, 'rb'), delimiter=',')
for row in csvReader:
    a.append(row)

for row in a:
    if row[0] == "date format then": <= here I need some pattern filter but I don't know how to do it
        print 'yes'

Date is always in format %Y-%m-%d . 日期始终采用%Y-%m-%d格式。

You can use the datetime module to check this: 您可以使用datetime模块进行检查:

import datetime
a = []
csvReader = csv.reader(open(csv_file_to_open, 'rb'), delimiter=',')
for row in csvReader:
    a.append(row)

for row in a:
    try:
        datetime.datetime.strptime(row[0],'%Y-%m-%d')
        print 'yes'
    except ValueError:
        continue

This should do it! 这应该做!

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

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