在Python裡,這個JavaScript 替換怎麼寫呢?

時間 2021-05-29 22:29:25

1樓:pcat

其實搜尋下就知道,python的分組是使用\1# -*- coding:utf8 -*-import

res1

='2018-08-08's2=

re.sub(

'(\d)-(\d)-(\d)',r

'\1年\2月\3日',s1

)# 如果漢字顯示有問題,用下面這個

# s2 = re.sub('(\d)-(\d)-(\d)',u'\\1年\\2月\\3日',s1)

print(s2)

2樓:fasionchan

通往羅馬的道路不止一條。

如果你不用那麼嚴謹,採用最簡單的字串分割即可:

>>>s=

'2018-08-08'

>>>s.

split

('-')[

'2018'

,'08'

,'08'

]>>>

'{}年{}月{}日'

.format(*

s.split

('-'

))'2023年08月08日'

如果想要嚴謹點,可以用正則,確保年月日都是數字,但不能檢測非法月日(例如13月):

>>> s = '2018-08-08'

>>> re.match('(\d+)-(\d+)-(\d+)', s).groups()

('2018', '08', '08')

>>> '{}年{}月{}日'.format(*re.match('(\d+)-(\d+)-(\d+)', s).groups())

'2023年08月08日'

>>> s = '2018-08-08'

>>> from datetime import datetime

>>> datetime.strptime(s, '%Y-%m-%d')

datetime.datetime(2018, 8, 8, 0, 0)

>>> datetime.strptime(s, '%Y-%m-%d').strftime("%Y年%m月%d日")

'2023年08月08日'

MATLAB 裡有什麼函式在 Python 裡是找不到的?

某些內建函式的實現,python裡無法確定有沒有,比如一些平滑函式的平滑核。原始碼是不公開的,所以理論上是 找不到的 除非matlab公布原始碼對比。 matlab 的 dbstop if error 執行時自動將斷點打在報錯的語句前簡直除錯神器目前沒發現python有類似功能 王王 我記得ldl沒...

在python裡,禁用 以及is和in,如何判斷兩個數字的值是否相等?

def compare number x,y if bool int x int yreturn Falsereturn True def compare number 2 x,y if bool int x int yreturn Falsereturn True 這麼老的題目居然出現在我的時間線...

Python 這個問題怎麼解決?

簡單,利用列表推導式。name list 張三 王五 李四 王六七 王仁 ifi startswith 王 elsei fori inname list 張三 王仁 李四 王仁 定義變數A,預設有3個元素 A xiaoWang xiaoZhang xiaoHua print 修改之前,列表A的資料 ...