python3

参考资料

官方中文文档
https://docs.python.org/zh-cn/3.8/

Python2.x与3​​.x版本区别

https://www.runoob.com/python/python-2x-3x.html

https://learntocodewith.me/programming/python/python-2-vs-python-3/

https://www.cnblogs.com/relex/p/10953912.html

print函数的使用,python3必须加括号,python2加不加都行。
py2 的默认编码是ASCII,py3的默认编码是UTF-8。

centos 安装 python3

root@10.13.32.65:/data1/all-dpool-cron # yum install python3
base                                                                                                                                                                                 | 3.6 kB  00:00:00     
epel                                                                                                                                                                                 | 4.7 kB  00:00:00     
extras                                                                                                                                                                               | 2.9 kB  00:00:00     
updates                                                                                                                                                                              | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.6.8-13.el7 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.6.8-13.el7 for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: python3-pip for package: python3-3.6.8-13.el7.x86_64
--> Processing Dependency: libpython3.6m.so.1.0()(64bit) for package: python3-3.6.8-13.el7.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.6.8-13.el7 will be installed
---> Package python3-pip.noarch 0:9.0.3-7.el7_7 will be installed
---> Package python3-setuptools.noarch 0:39.2.0-10.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

============================================================================================================================================================================================================
 Package                                                 Arch                                        Version                                             Repository                                    Size
============================================================================================================================================================================================================
Installing:
 python3                                                 x86_64                                      3.6.8-13.el7                                        base                                          69 k
Installing for dependencies:
 python3-libs                                            x86_64                                      3.6.8-13.el7                                        base                                         7.0 M
 python3-pip                                             noarch                                      9.0.3-7.el7_7                                       updates                                      1.8 M
 python3-setuptools                                      noarch                                      39.2.0-10.el7                                       base                                         629 k

Transaction Summary
============================================================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

python 读取 cat 文件内容

方法1:
ng80_pid = "/tmp/tmp.txt"
for index, value in enumerate(iplist):
    if os.path.isfile(ng80_pid):
        pid = shell_command("cat %s" % ng80_pid)
        pid = str(pid, encoding="utf-8").strip()


方法2:
def pcat_oneline(path):
    result = ""
    with open(path) as f:
        result = f.readlines()[0].strip()
    return result

格式化字符串

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

这种f、引号和{} 的组合相当于告诉Python:“嘿,这是一个格式化字符串,把这些变量放到那几个位置。
my_weight = 200
print(f"He's {my_weight} pounds heavy.")

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

hilarious = False
joke_evaluation = "Isn't that joke so funny?! {}"
print(joke_evaluation.format(hilarious))

formatter = "{} {} {} {}"
print(formatter.format(
    "Try your",
    "Own text here",
    "Maybe a poem",
    "Or a song about fear"
))

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

w = "This is the left side of...
e = "a string with a right side."
print(w + e)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

days = "Mon Tue Wed Thu Fri Sat Sun"
print("Here are the days: ", days)
print("""
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")