如何区分字符串中的日期,月份和年份?

时间:2017-10-25 16:02:54

标签: python python-3.x split

我有一个问题:如何使用" strip"用于切割日期的功能,例如" 24.02.1999"? 输出应该是这样的&#; 24'' 02',' 1999'。

你能帮忙解决这个问题吗?

5 个答案:

答案 0 :(得分:1)

你可以这样做

    >>> stri="24.02.1999"
    >>> stri.split('.')
    ['24', '02', '1999']
    >>> 

答案 1 :(得分:0)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-responsive grid-table" id="test"> <thead> <tr> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Folio</span> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Banco Destino</span> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Tipo de documento</span> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Estado</span> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Conforme</span> &nbsp;&nbsp; <input type="checkbox" id="checkConforme"> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">No conforme</span> &nbsp;&nbsp; <input type="checkbox" id="checkNoConforme"> </th> <th class="" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">Comentarios</span> </th> <th class=" hidden" style="text-align:left; background-color:#ffffff;border: 1px solid #cccaca; color:#27a2fb; font-family:Helvetica; font-size:10.5pt"> <span class="mvc-grid-header-title">lolol</span> </th> </tr> </thead> <tbody> <tr> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000793</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Copia certificada</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ENTREGADO</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|1| " name="check" id="conforme"></td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000793|0| " name="check" id="noConforme"></td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td> <td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"> </td> </tr> <tr> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">2017100000790</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">058 - Banregio</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">Imagen</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt">ATENDIDO</td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|1|" name="check" id="conforme"></td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="checkbox" class="chb" value="2017100000790|0|" name="check" id="noConforme"></td> <td style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"><input type="text" class="lol" style="width:100%" value="" name="comentarioCierre" id="comentarioCierre" pattern="^[A-Za-zÀ-úÑñ 0-9]{1,}$"></td> <td class="hidden" style="font-family:Helvetica;border: 1px solid #cccaca; font-size:10pt"></td> </tr> </tbody> </table>用于删除字符。你的意思是strip。对于您的代码,

split

输出:

date = input('Enter date in the format (DD.MM.YY) : ')
dd, mm, yyyy = date.strip().split('.')
print('day = ',dd)
print('month = ',mm)
print('year = ',yyyy)

答案 2 :(得分:0)

您需要使用split()而不是strip()

strip()用于从字符串中删除指定的字符。

split()用于根据提供的值将字符串拆分为列表。

date = str(input()) # reading input date in dd.mm.yyyy format
splitted_date = date.split('.') # splitting date 

day = splitted_date[0] # storing day
month = splitted_date[1] # storing month
year = splitted_date[2] # storing year

# Display the values
print('Date : ',date)
print('Month : ',month)
print('Year : ',year)

您可以按照DD.MM.YYYY格式分割日期。

答案 3 :(得分:0)

你去吧

 date="24.02.1999"
 [dd,mm,yyyy] = date.split('.')
 output=(("'%s','%s','%s'") %(dd,mm,yyyy))
 print(output)

替代方式

 date="24.02.1999"
 dd=date[0:2]
 mm=date[3:5]
 yyyy=date[6:10]
 newdate=(("'%s','%s','%s'") %(dd,mm,yyyy))
 print(newdate)

另一种替代方式

from datetime import datetime

date="24.02.1999"
date=datetime.strptime(date, '%d.%m.%Y')
date=(("'%s','%s','%s'") %(date.day,date.month,date.year))
print(date)

享受

答案 4 :(得分:0)

您应该使用datetime.strptime(..)将字符串转换为datetime object,而不是拆分字符串:

dokku ps:rebuild staging

然后您可以访问所需的值:

>>> from datetime import datetime
>>> my_date_str = "24.02.1999"
>>> my_date = datetime.strptime(my_date_str, '%d.%m.%Y')