Python snake_case to CamelCase and hungarian
I have recently been working on a Flask application, and needed a systematic way to convert the snake case (underscored) column names being returned by a database cursor into hungarian case to return as JSON. I ended up writting the following two functions to convert snake case to hungarian and camel caseing respectively. I hope you find these helpful.
snake_case to hungarianCase
def to_hungarian(text):
#Start off with lower cased word:
text = text.lower()
converted_text = ''
words = text.split('_')
if len(words) == 1:
converted_text = text
else:
#Use lower cased first word followed by proper cased words after
converted_text = words[0]
for word in words[1:]:
converted_text += word.title()
return converted_text
#### snake_case to CamelCase
def to_camel_case(text):
#Start off with lower cased word:
text = text.lower()
converted_text = ''
words = text.split('_')
for word in words:
converted_text += word.title()
return converted_text