Camel turn underscore the variable name type python

for example

The simplest idea :
only consideration, is a direct copy of the current character , or the first plus '_' and then copy the current character .

#!/usr/bin/env python3
def change_variable_name(listx):    
    listy = listx[0]
    for i in range(1,len(listx)):
        # listx[i] 直接copy 或 先加'_'再copy
        if listx[i].isupper() and not listx[i-1].isupper():# 加'_',当前为大写,前一个字母为小写
            listy+='_'
            listy+=listx[i]
        elif listx[i].isupper() and listx[i-1].isupper() and listx[i+1].islower():
              # 加'_',当前为大写,前一个字母为小写
            listy+='_'
            listy+=listx[i]
        else:
            listy+=listx[i]
    return listy.lower()

listx = 'oneHTTPRequestAaureBBBXu'# for example
print(change_variable_name(listx)) # output: one_http_request_aaure_bbb_xu

Guess you like

Origin blog.csdn.net/CallMeYunzi/article/details/83273078