Python memo using perl --format is convenient.

--Make a note of what you thought was python.

Variable expansion by%

--The equivalent of sprintf in perl can be achieved with% in python. It's easy and good.

>>> u"%s,%02d" % ('ABC',2)
'ABC,02'

There was also a format.

It seems that it can be expanded with {subscript}.

>>> "{0},{1}".format('ABC',2)
'ABC,2'

It seems that you can also specify a format like% 02d.

>>> "{0},{1:02d}".format('ABC',2)
'ABC,02'

Is it a list expansion?

――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'

Convenient combination with dictionary type!

――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!

Example of dictionary type and format combination

――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
〜〜〜

With perl ...

――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

Python memo using perl --format is convenient.
Python memo using perl --join
Python memo using perl-Dictionary type (case is not valid)
Python Note: About comparison using is
Python memo
python memo
Python memo
python memo
Python memo
Python memo
Python memo
Hash in Perl is a dictionary in Python
[Python] Complete preprocessing Memo as it is
Download files in any format using Python
[Python] Memo dictionary
[Personal memo] julia --Using Python library with julia using PyCall
Python is easy
python beginner memo (9.2-10)
Start using Python
★ Memo ★ Python Iroha
[Python] Ravel () is convenient when drawing multiple graphs
[Python] EDA memo
Python 3 operator memo
What is python
Python is instance
[My memo] python
Python3 metaclass memo
Python string format
[Python] Basemap memo
format in python
Python beginner memo (2)
Scraping using Python
[Python] Numpy memo
What is Python
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
The pain of gRPC using Python. November 2019. (Personal memo)
Minimum memo when using Python on Mac (pyenv edition)
Memo for building a machine learning environment using Python
Ssh connection memo using ProxyCommand of ssh_config in Python
My python environment memo
python variable expansion, format
Python module (Python learning memo ④)
Operate Redmine using Python Redmine
Visualization memo by Python
[Python] What is Pipeline ...
Convenient Python methods, etc.
Python test package memo
[Python] Memo about functions
Data cleaning using Python
python regular expression memo
Binary search (python2.7) memo
Image format in Python
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
Using Python #external packages
Python Pickle format notes
DynamoDB Script Memo (Python)
[Python] format methodical use