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

最新文章

  1. nandaom
  2. java反射小例子
  3. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
  4. sharding-jdbc-how2work 当当的sharding-jdbc剖析(查询)
  5. No message found under code ' for locale 'en'.
  6. Maven——聚合与继承
  7. java之enum枚举(2015年05月28日)
  8. Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)
  9. StringBuilder 大量字符串时使用,速度比较快
  10. 选择LDO的方法(转)
  11. Why attitude is more important than IQ
  12. python之字典、列表、元组生成器的使用
  13. C#生成COM组件
  14. Docker实践:python应用容器化
  15. 深度学习论文翻译解析(一):YOLOv3: An Incremental Improvement
  16. Unity PC端发布失败解决办法
  17. c# 登录 防止sql注入 mysql数据库
  18. LFM隐语义模型Latent Factor Model
  19. kernel cmdline
  20. 解决Cocos2d-x编译错误: 无法打开 源 文件 "extensions/ExtensionExport.h"

热门文章

  1. HDU-3549Flow Problem 最大流模板题
  2. HDU2485Destroying the bus stations 拆点网络流求割点个数
  3. hdu 6092 Rikka with Subset(多重背包)
  4. codeforces 486 E. LIS of Sequence(dp)
  5. 【Offer】[3-1] 【找出数组中重复的数字】
  6. 05 python内置函数大全
  7. SQL Server2008 并发数测试
  8. odoo12从零开始:三、2)odoo模型层
  9. 仿QQ5.0侧滑菜单
  10. kafka入门配置