What I learnt …. uuuuuggghhhhhh some fantastic staffff!!
Check it out dute:
- replacement field. So if I want to add something I can use format() method
#this is a replacement field acions
# use of format() method
u = ‘Filip’
pas = ‘trum tum tum’
print ‘{0} is an owner of {1} password’.format(u, pas)
and you are getting
>>> Filip is an owner of trum tum tum password
Another mojo bojo
# using format() method in list
fixes = [‘KB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’, ‘EB’, ‘ZB’, ‘YB’]
# here I am using {0} – mean first list in fixes
# {0[1]} – mean
first list and second item in that first list
print ‘1000{0[0]} = 1{0[1]}’.format(fixes)
>>> 1000KB = 1MB
# great method to extract data from string
# split() method I can use only if the data is in some schema
query = ‘user=pilgrim=123&database=master&password=PapayaWhip’
a_list = query.split(‘&’)
print a_list
# elem.split() method used for each element in list
# remember if use the number ‘1’ in spliting otherwise if there will be more ‘=’ you will get ERROR!
a_list_of_lists = [v.split(‘=’,1) for v in a_list if ‘=’ in v]
print a_list_of_lists
#making dict from list
a_dict = dict(a_list_of_lists)
print a_dict
>>>[‘user=pilgrim=123’, ‘database=master’, ‘password=PapayaWhip’]
>>>[[‘user’, ‘pilgrim=123’], [‘database’, ‘master’], [‘password’, ‘PapayaWhip’]]
>>>{‘password’: ‘PapayaWhip’, ‘user’: ‘pilgrim=123’, ‘database’: ‘master’}
No comments:
Post a Comment