This article is to help a group of friends answer a question.
他有一個需求,就是對於日期的錄入都是中文形式的,需要轉換為數字形式的.由於python庫中沒有函數直接進行轉換,因此我寫了本篇文章.
for the code in this article,如果有不懂的地方,可以留言說明,我會解答.
df = pd. read_excel( r"C:\Users\黃偉\Desktop\chinese_english.xlsx")
display( df)
def func( x):
year = x. split( "年")[ 0]
month = x. split( "年")[ 1]. split( "月")[ 0]
day = x. split( "年")[ 1]. split( "月")[ 1]. split( "日")[ 0]
if len( day) >= 3:
day = day[ 0] + day[ 2]
chinese_english = dict( 零 = 0, 一 = 1, 二 = 2, 三 = 3, 四 = 4, 五 = 5, 六 = 6, 七 = 7, 八 = 8, 九 = 9, 十 = 10)
year = "". join( str( chinese_english[ i]) for i in year)
month = "". join( str( chinese_english[ i]) for i in month)
day = "". join( str( chinese_english[ i]) for i in day)
if len( month) == 3:
month = month[ 0] + month[ 2]
if len( day) == 3:
day = day[ 0] + day[ 2]
final_date = year + "." + month + "." + day
return final_date
df[ "final_date"] = df[ "日期"]. apply( func)
display( df)
結果如下: