宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取
1.rangex,y)
[x,y)
>>> range0,4) #0,1,2,3
>>> range1,4) #1,2,3
2.dics
dics.getkey, default=None)
当key在dics中不存在时返回default的值,返回一个函数也是可以的
>>> dics = {
0 : 'a',
1 : 'b',
2 : 'c'
}
>>> dics.get1, 'not found')
'b'
>>> dics.get4, 'not found')
'not found'
判断key在dic中是否存在
两种方法
__contains__'key')
'key' in d.keys)
>>> d = {'name':'jack', 'sex':1, 'age':33}
>>> d
{'name': 'jack', 'sex': 1, 'age': 33}
>>> d.__contains__'name')
True
>>> 'name' in d.keys)
True
foreach一个dic
j = {'key1' : 'value1', 'key2' : 'value2'}
for key, value in j.items): # items)中每一项都是 tuple 类型,这里的key和value不是特定的关键字
printkey)
printvalue)
# key1
# value1
# key2
# value2
单独循环key或者values
for k in j.keys):
printk)
for v in j.values):
printv)
3.读写文件
python中文件被分为两种,text or binary
text
读到的字符串经过了编码
每一行都以EOLEnd of Line)结尾
on Unix
on Windows
在写入的字符串中加入’
‘会自动换行
binary
在mode后加上’b’
二进制方式读写文件
基本流程
open
write | read
close
with 相当于 using,自动close)
# mode r w aappend) r+read and write)
with open'filepath','mode', encoding = 'utf-8') as file:
file.write'aaa')
file.readsize) # 读size个字符或byte
file.readline)
# from_what 0 default)beginning of the file
# 1 current file position
# 2 end of file
file.seekoffset, from_what)
file.readlines)
写json
import json
with open'filepath', 'w', encoding = 'utf-8') as file:
data = json.dumpsu'data', ensure_ascii = False)
file.writeunicodedata))
python的json库只能序列化python的内置类型,如果是自定义的数据结构无法使用json.dumps序列化
可以使用jsonpickle
import jsonpickle
class Thingobject):
def __init__self, name):
self.name = name
obj = Thing'Awesome')
jsn = jsonpickle.encodeobj) # 序列化,会带上类型信息用于反序列化
jsonpickle.decodejsn) # 反序列化
jsn = jsonpickle.encodeobj, unpicklable=False) #序列化,不带类型信息
4. 判断某个class是否存在某个attribute
hasattrclass_a, 'attribute')
5. 判断某个变量是否是list
a = [1, 2, 3]
if isinstancea, list):
print'a is a list')
6. list.append),引用传递
在li.appenda)
之后对a进行修改,li中的数据也会改变
此时的a与li[0]指向同一块内存
>>> import scrapy
>>>
>>> class Ascrapy.Item):
... post_id = scrapy.Field)
... author_id = scrapy.Field)
>>>
>>> a = Apost_id = "post_id_1", author_id = "author_id_1")
>>> li = []
>>> li.appenda)
>>> printli)
[{'author_id': 'author_id_1', 'post_id': 'post_id_1'}]
>>> a['post_id'] = 'post_id_2'
>>> printli)
[{'author_id': 'author_id_1', 'post_id': 'post_id_2'}]