Transform snake_case to camelCase

I want to convert db columns such as is_deleted or product_code which consist of xx_xx to java-style names: isDeleted or productCode. I wonder if there is a way to improve my following code, using regex or such things.

def getJavaName(column):
words = column.split('_')
first = words[0]
return first + ''.join((w.capitalize() for w in words[1:]))

• Since this is under the code review section, I'll provide the answer I would in a real code review. Don't do it at all. Doing a refactoring of naming in this way has a fair chance of introducing bugs with no improvement in functionality. AKA: If it ain't broke don't fix it. Spend the time you save working on new features or fixing real problems. – user69264 Mar 30 '15 at 16:24
• @user69264 Thanks for practical suggestions. Actually，I wrote this script replace an Eclipse plugin used in my work, also for purpose——leaning python :) – Mark Soul Apr 2 '15 at 5:21
• Actually this type of name refactoring has been going on for years in the Client-Server, SOA, and ETL spaces. It is not so much to rename columns or alter DDL, but to create mappings of the objects as structs and such. So if I am working in C, I want the naming conventions and familiar look and feel. This matters still as there is a lot of Java-DB2 going on. Not to mention Django. – mckenzm May 18 '15 at 1:29

You should remove the many trailing spaces.

You can replace slicing with unpacking and the double-brackets can be removed:

def getJavaName(column):
first, *rest = column.split('_')
return first + ''.join(word.capitalize() for word in rest)


Python uses snake_case, even if your DB columns don't, so you should be calling this get_java_name - although personally it's still better named snake_to_camel_case and the column parameter should be renamed to, say, name.

• I like the way you are doing the extraction. nice. – bhathiya-perera Mar 29 '15 at 14:21
• first, *rest = column.split('_') doesn't work in Python 2.x. – Nizam Mohamed Mar 29 '15 at 17:35
• @NizamMohamed you can use first, rest = column.split('')[0], column.spilt('')[1:] in Python 2.x – Mark Soul Mar 30 '15 at 5:12
• Thanks for this, neat little trick for manipulating the result array – Daniel Dubovski Apr 29 at 12:25

Using regex,

import re
def getJavaName(column):
return re.sub('_.',lambda x: x.group()[1].upper(),column)


Consider writing this as a one-liner regular expression substitution instead.

import re

def get_java_name(column_name):
return re.sub('_([a-z])', lambda match: match.group(1).upper(), column_name)


Since the function is Python code, it should still adhere to PEP 8 naming conventions — get_java_name() instead of getJavaName().

• This fails for non-ASCII identifiers. You might argue that it's bad practice anyway, though ;). – Veedrac Mar 29 '15 at 19:17