簡體   English   中英

使用正則表達式和python從單個字符串中檢索不同的值

[英]retrieve different values from a single string with regular expressions and python

建議我有這個:

valuestringdate = "24/6/2010"

我想從變量中得到類似的東西

day = 24
month = 6
year = 2010

您可以split字符串:

day, month, year = valuestringdate.split('/')

使用.split()方法。 在這種情況下, dateList = valuestringdate.split("/")將產生列表: dateList = ["24","6","2010]

使用索引: day = dateList[0]將設置day = "24"

從那里,您可以使用day = int(day)將日期從字符串轉換為整數。

您應該能夠從那里弄清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM