Python 基础知识的记录

Python 基础知识的记录,第1张

字典和数据复制的使用
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:\Desktop\output_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打印后进行字符集分析

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/799193.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-06
下一篇2022-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存