[Python] Python 学习记录(2)

时间:2022-02-27 22:07:44

1.range(x,y)

[x,y)

>>> range(0,4) #0,1,2,3
>>> range(1,4) #1,2,3

2.dics

  • dics.get(key, default=None)

当key在dics中不存在时返回default的值,返回一个函数也是可以的

>>> dics = {
    0 : 'a',
    1 : 'b',
    2 : 'c'
}
>>> dics.get(1, 'not found')
'b'
>>> dics.get(4, '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不是特定的关键字
    print(key)
    print(value)

# key1
# value1
# key2
# value2

单独循环key或者values

for k in j.keys():
    print(k)

for v in j.values():
    print(v)

3.读写文件

python中文件被分为两种,text or binary

  • text

读到的字符串经过了编码

每一行都以EOL(End of Line)结尾

\n on Unix

\r\n on Windows

在写入的字符串中加入'\n'会自动换行

  • binary

在mode后加上'b'
二进制方式读写文件

基本流程

  1. open

  2. write | read

  3. close

with 相当于 using,自动close()

# mode r w a(append) r+(read and write)
with open('filepath','mode', encoding = 'utf-8') as file:
    file.write('aaa')
    file.read(size) # 读size个字符或byte
    file.readline()
    
    # from_what 0 (default)beginning of the file
    # 1 current file position
    # 2 end of file
    file.seek(offset, from_what)
    
    file.readlines()

写json

import json
with open('filepath', 'w', encoding = 'utf-8') as file:
    data = json.dumps(u'data', ensure_ascii = False)
    file.write(unicode(data))

python的json库只能序列化python的内置类型,如果是自定义的数据结构无法使用json.dumps序列化
可以使用jsonpickle

import jsonpickle

class Thing(object):
    def __init__(self, name):
        self.name = name

obj = Thing('Awesome')

jsn = jsonpickle.encode(obj) # 序列化,会带上类型信息用于反序列化

jsonpickle.decode(jsn) # 反序列化

jsn = jsonpickle.encode(obj, unpicklable=False) #序列化,不带类型信息

4. 判断某个class是否存在某个attribute

hasattr(class_a, 'attribute')

5. 判断某个变量是否是list

a = [1, 2, 3]
if isinstance(a, list):
    print('a is a list')

6. list.append(),引用传递

li.append(a)之后对a进行修改,li中的数据也会改变
此时的a与li[0]指向同一块内存

>>> import scrapy
>>>
>>> class A(scrapy.Item):
...     post_id = scrapy.Field()
...     author_id = scrapy.Field()
>>>
>>> a = A(post_id = "post_id_1", author_id = "author_id_1")
>>> li = []
>>> li.append(a)
>>> print(li)
[{'author_id': 'author_id_1', 'post_id': 'post_id_1'}]
>>> a['post_id'] = 'post_id_2'
>>> print(li)
[{'author_id': 'author_id_1', 'post_id': 'post_id_2'}]

Reference

  1. jsonpickle Documentation