




















创建于 2022年5月25日 17:42:46 · 约732字
[ 📝Note ]
积累一些技巧,比如读写超大文件这种小技巧
🕊python 进阶技能_哔哩哔哩_bilibili
Python 进阶视频_哔哩哔哩_bilibili
"a"+b+"c"使用"a{b}c"except:空Error═ ═检查布尔值,用is,用if检查bool直接if xfor i,v in enumerate(list)代替for i in range(len(list))获取索引+元素 for i,(v1,v2) in enumerate(zip(list1,list2))x,y=atupletime.perf_counter时钟时间代替time.time更加精确shell=True,这会让进程在shell里运行(他是这么说的,但是我也不知道是啥问题)import *会命名空间混乱驼峰法使用大小写来命名变量的方法,分为大驼峰和小驼峰
特点 大驼蜂: UserName
小驼蜂userName
循环记得打括号
#coding=utf-8#-*- coding: utf-8 -*-
一直搞不清楚这俩头什么区别。直觉上一样。今天特意查了一下,大致明白了:
对于python解释器来说,这两种写法一样。但是对于编辑器来讲,可能会出现识别问题。
参考PEP-0236 Defining the Encoding
Python will default to ASCII as standard encoding if no other encoding hints are given.
To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as:
# coding=<encoding name>
or (using formats recognized by popular editors)
#!/usr/bin/python
# -*- coding: <encoding name> -*-
or
#!/usr/bin/python
# vim: set fileencoding=<encoding name> :
More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". The first group of this
expression is then interpreted as encoding name. If the encoding
is unknown to Python, an error is raised during compilation. There
must not be any Python statement on the line that contains the
encoding declaration.
To aid with platforms such as Windows, which add Unicode BOM marks
to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comment is given).
If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'. Any other encoding will cause an error.
类中的一些函数没有返回值的时候,可以返回self使用方法链,如果是不修改自身实例,返回一个新的,可以使用type创建新实例使用方法链
python
class Player:
def __init__(self,name,x,y):
self.name=name
self.x=x
self.y=y
def zero(self):
self.x=0
self.y=0
return self # 返回自身,使用链式调用
def moveX(self,offset):
self.x+=offset
return self # 返回自身,使用链式调用
# return type(self)(self.name,self.x+offset,self.x) # 不改变自身返回新对象的链式调用
def moveY(self,offset):
self.y+=offset
return self # 返回自身,使用链式调用
def __str__(self):
return "%s:(%s,%s)"%(self.name,self.x,self.y)
p=Player("player1",5,5)
(
p.zero() # 也可以用\
.moveX(3)
.moveY(1)
)
# 等价于 p.zero().moveX(3).moveY(1)
print(p)运行结果
python
date_obj = datetime.strptime(datetime_str,'%Y-%m-%dT%H:%M:%S.%fZ') # 尝试将字符串解析为ISO日期
result_str = date_obj.strftime('%Y-%m-%d') # 将日期转换为所需的格式'%y-%m-%d'此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。