how can i rename key value in python?
i have this code :
t = { u'last_name': [u'hbkjh'], u'no_of_nights': [u'1'], u'check_in': [u'2012-03-19'], u'no_of_adult': [u'', u'1'], u'csrfmiddlewaretoken': [u'05e5bdb542c3be7515b87e8160c347a0'], u'memo': [u'kjhbn'], u'totalcost': [u'1800.0'], u'product': [u'4'], u'exp_month': [u'1'], u'quantity': [u'2'], u'price': [u'900.0'], u'first_name': [u'sdhjb'], u'no_of_kid': [u'', u'0'], u'exp_year': [u'2012'], u'check_out': [u'2012-03-20'], u'email': [u'ebmalifer@agile.com.ph'], u'contact': [u'3546576'], u'extra_test1': [u'jknj'], u'extra_test2': [u'jnjl'], u'security_code': [u'3245'], u'extra_charged': [u'200.0']}
key = {str(k): str(v[0]) for k,v in t.iteritems() if k.startswith('extra_')}
array = []
for val in key:
data = str(val) + ' = ' + key[val] + ','
array.append(data)
print array
it give me this :
["extra_charged = 200.0,", "extra_test1 = jknj,", "extra_test2 = jnjl,"]
what should i do to remove the 'extra_'
and it makes the output like this:
["CHARGED = 200.0,", "TEST1 = jknj,", "TEST2 = jnjl,"]
can anyone have an idea about my case? thanks in advance ...
So, array indexing can strip off the first 6 characters, and upper() should uppercase it.
Replace that one data= line with:
data = str(val)[6:].upper() + ' = ' + key[val] + ','
that should work.
i found this .replace()
and i do like this ..
data = str(val).replace("extra_","").upper() + ' = ' + key[val] + ','
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.