camelcasing - Elegant Python function to convert CamelCase to snake_case? -
उदाहरण:
& gt; & gt; & gt; कन्वर्ट ('कैमल सीज़') 'ऊमकैकेज'
यह बहुत पूर्ण है:
<पूर्व> def (नाम): s1 = re.sub ('(।) ([AZ] [az] +)', r '\ 1_ \ 2', नाम) रिटर्न re.sub (' ([A-z0- 9]) ([AZ]) ', r' \ 1_ \ 2 ', s1)। कमांड ()
इन सभी के साथ काम करता है (और नहीं हानि पहले से ही- un-cameled संस्करण):
& gt; & gt; & gt; कन्वर्ट ('कैमल कैस') 'ऊमल कैकेस' & gt; & gt; & gt; कन्वर्ट ('कैमल कैमेल कैस') 'यूएमएल_camel_case' & gt; & gt; & gt; कन्वर्ट ('Camel2Camel2Case') 'camel2_camel2_case' & gt; & gt; & gt; कन्वर्ट ('getHTTPResponseCode') 'get_http_response_code' & gt; & gt; & gt; कन्वर्ट ('get2HTTPResponseCode') 'get2_http_response_code' & gt; & gt; & gt; कन्वर्ट ('HTTPResponseCode') 'http_response_code' & gt; & gt; & gt; ('HTTPResponseCodeXYZ') 'http_response_code_xyz'
या यदि आप उसे एक ज़िलिन बार कॉल करने जा रहे हैं, तो आप regexes को पूर्व संकलित कर सकते हैं:
प्रथम_कैप_रे = री कॉमपाइल ('(।) ([AZ] [az] +)') all_cap_re = re.compile ('([a-z0- 9]) ([AZ])' डीईएफ़ कन्वर्ट (नाम) : S1 = first_cap_re.sub (r '\ 1_ \ 2', नाम) all_cap_re.sub (r '\ 1_ \ 2', s1) लौटें। ()
नहीं नियमित अभिव्यक्ति मॉड्यूल आयात करना भूल जाएं
import re
Comments
Post a Comment