栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python 基础知识的记录

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python 基础知识的记录

字典和数据复制的使用
user_dict = {"hans":"active","element":"unactive","zhans":"active"}
for single_user,single_value in user_dict.copy().items():
    print(single_user,"-->",single_value)

在字典中的keys、values、items分别对应了键、值和其组合,在使用中可用for循环语句进行调用,copy函数的使用需要引起注意。

def concat(*args,sep = "/"):
    return sep.join(args)

print(concat("earth","mars","venus"))
"--->"
print("/".join(["earth","mars","venus"]))

数据的拼合常用的函数的组合,可以复用,join的使用适用于单独使用,"+"的使用不建议,会引起不必要的麻烦。

yes_votes = "42_09_87"
no_votes = 12344.233
percentage = "yes_votes second paragraph is {},and no_votes decimal point is {}".format(yes_votes.split("_",1),
                                                                                        round(no_votes,2))
print(percentage)

数据的引用format等较为常用,其他的不建议使用

from datetime import date
import logging
now = date.today()
birthday = date(1990,5,5)
age = now - birthday
print(age.days)
print(logging.info("Informational message"))

date的日期标准库使用需要注意,会给自己省了很多精力。

#encoding = "utf-8"
with open("C:Desktopoutput_wrong_cases.txt",encoding='utf-8') as f:
    for line in f:
        print(line,end = "")

文本的读写经常出现的问题是字符集问题,内部的encoding使用会解决很多诸如"UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa1 in position 80: illegal multibyte sequence
"的问题,如果无法避免建议使用try进行容错,加上print打印后进行字符集分析

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/858955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号