--Make a note of what you thought was python.
--The equivalent of sprintf in perl can be achieved with% in python. It's easy and good.
>>> u"%s,%02d" % ('ABC',2)
'ABC,02'
>>> "{0},{1}".format('ABC',2)
'ABC,2'
>>> "{0},{1:02d}".format('ABC',2)
'ABC,02'
――I will try to enter a variable instead of entering it directly.
>>> a = ['a','b','c']
>>> a
['a', 'b', 'c']
>>> "{0}_{1}_{2}".format(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> "{0}_{1}_{2}".format(*a)
'a_b_c'
>>> "{0}_{1}_{2}".format(*['a','b','c'])
'a_b_c'
>>> "{0}_{1}_{2}".format(*('t','u','p'))
't_u_p'
――Well, that's right up to this point, isn't it? ――But the combination with the dictionary type is convenient, it seems that you are using the template module only with this. ――It seems that you can write \ * as two variables, \ * \ * d.
>>> d={"name":"John","age":25}
>>> d
{'name': 'John', 'age': 25}
>>> "call me '{name}' , I'm {age}".format(**d)
"call me 'John' , I'm 25"
--The Easy!
――At this time, there are only dictionaries and arrays like JSON, so the range of applications is wide.
rss.py
# -*- coding: utf-8 -*-
import feedparser
qiita_dic = feedparser.parse('http://qiita.com/tags/python/feed.atom')
for entry in qiita_dic.entries:
print(u"{title}\t{link}\t{published}\t{author}".format(**entry))
――It is the execution result, it is good to be able to do it in one line!
sh-3.2$ python rss.py
TensFlow Preferences Memo http://qiita.com/s_nakamura/items/b502fb29b3f6321a0403 2016-10-10T16:09:52+09:00 s_nakamura
I tried to make Othello AI with tensorflow without understanding the theory of machine learning ~ Part 1 ~ http://qiita.com/sasaco/items/3b0b8565d6aa2a640caf 2016-10-10T15:28:54+09:00 sasaco
[Personal notes]Python sequence type / mapping type http://qiita.com/RyoMa_0923/items/a714eb5dce24e9463c00 2016-10-10T13:16:19+09:00 RyoMa_0923
Let's summarize the Python coding standard PEP8(2) http://qiita.com/sartan123/items/a74010b06f47792e7660 2016-10-10T11:55:54+09:00 sartan123
StackStorm :Develop pack with st2sdk http://qiita.com/unchemist/items/10046264d29fb7c7334b 2016-10-10T03:08:35+09:00 unchemist
Implement features like window replacement on Ubuntu http://qiita.com/fx-kirin/items/f41aaaeef9a5886cf87f 2016-10-10T00:50:32+09:00 fx-kirin
〜〜〜
――It looks like this, isn't it intuitive compared to format ...
sh-3.2$ perl -le '{my %d = ("name"=>"John","age"=>25);print sprintf("call me %s , Im %d",map {$d{$_}} ("name","age"))}'
call me John , Im 25
Recommended Posts