def parse_accept_language_header(string, pattern='([a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})?)\\s*(;\\s*q\\s*=\\s*(1|0\\.[0-9]+))?'):
 res = {}
 if (not string):
  return None
 for match in re.finditer(pattern, string):
  if (None == match.group(4)):
   q = 1
  else:
   q = match.group(4)
  l = match.group(1).replace('-', '_')
  if (len(l) == 2):
   l = l.lower()
  elif (len(l) == 5):
   l = ((l.split('_')[0].lower() + '_') + l.split('_')[1].upper())
  else:
   l = None
  if l:
   res[l] = int((100 * float(q)))
 return res

def parse_accept_language_header ( string , pattern = '([a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})?)\\s*(;\\s*q\\s*=\\s*(1|0\\.[0-9]+))?' ) : res = { } if ( not string ) : return None for match in re . finditer ( pattern , string ) : if ( None == match . group ( 4 ) ) : q = 1 else : q = match . group ( 4 ) l = match . group ( 1 ) . replace ( '-' , '_' ) if ( len ( l ) == 2 ) : l = l . lower ( ) elif ( len ( l ) == 5 ) : l = ( ( l . split ( '_' ) [ 0 ] . lower ( ) + '_' ) + l . split ( '_' ) [ 1 ] . upper ( ) ) else : l = None if l : res [ l ] = int ( ( 100 * float ( q ) ) ) return res
parse a dict from an accept-language header string example input: en-us .