随笔分类 -  Python

摘要:id id(x)对应变量x所引用对象的内存地址。可以把id(x)看成变量x的身份标识。 is 有时在编程中需要与变量的身份标识打交道,但不是通过 id 函数,而是 is 操作符。 The operators is and is not test for object identity: x is y 阅读全文
posted @ 2017-09-22 10:41 Sawyer Ford 阅读(237) 评论(0) 推荐(0)
摘要:Python中的变量有两个特点: 1. 无需声明 2. 不与类型绑定 变量名只是内存中具体对象的一个引用(reference)。 对于 a = 1,内存模型如下: 对于 内存模型如下: 可以通过id(x)获取变量x所引用对象的内存地址 输出如下: address of the object refe 阅读全文
posted @ 2017-09-21 21:59 Sawyer Ford 阅读(113) 评论(0) 推荐(0)
摘要:初始化 迭代 迭代时删除 参考文献 Remove items from a list while iterating How to delete items from a dictionary while iterating over it? 阅读全文
posted @ 2017-09-21 17:08 Sawyer Ford 阅读(138) 评论(0) 推荐(0)
摘要:python脚本如何调用Java的jar文件呢? HelloWorld.java javacaller.py 输出: b'hello world\n' <class 'bytes'>decode: hello world <class 'str'>b'LOGAN' <class 'bytes'>de 阅读全文
posted @ 2017-08-15 14:29 Sawyer Ford 阅读(543) 评论(0) 推荐(0)
摘要:今天发现,使用redis-py从redis中获取的数据竟然是加密的。 输出: result: b'value1'result type: <class 'bytes'> result_decoded: value1result_decoded type: <class 'str'> result_s 阅读全文
posted @ 2017-08-11 21:09 Sawyer Ford 阅读(197) 评论(0) 推荐(0)
摘要:with 语句适用于对资源进行访问的场合,确保不管使用过程中是否发生异常都会执行必要的“清理”操作,释放资源。 比如文件使用后自动关闭、线程中锁的自动获取和释放等。 运行机制 等价于 VAR对应一个上下文管理器(context manager)。 上下文管理器 实现了__enter__()和__ex 阅读全文
posted @ 2017-08-08 21:44 Sawyer Ford 阅读(175) 评论(0) 推荐(0)
摘要:用Python实现一个http server python post json的两种方法: 阅读全文
posted @ 2017-08-07 20:51 Sawyer Ford 阅读(3992) 评论(0) 推荐(0)
摘要:字符串处理绝对是任何一门语言的重点。 str.partition(sep) Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, 阅读全文
posted @ 2017-08-06 12:04 Sawyer Ford 阅读(196) 评论(0) 推荐(0)
摘要:decorator(装饰器)绝对是Python学习道路上的一个拦路虎。想要理解它,先看两个概念。 nested function(内嵌函数) 类似于函数局部变量,内嵌函数就是函数的局部函数。 Python的内嵌函数有个特点,它可以访问自身作用域外围的变量。 内嵌函数is_even可以直接访问外围参数 阅读全文
posted @ 2017-08-05 15:08 Sawyer Ford 阅读(175) 评论(0) 推荐(0)
摘要:time 输出: timestamp:1505887820.714079, type: <class 'float'>localtime: time.struct_time(tm_year=2017, tm_mon=9, tm_mday=20, tm_hour=14, tm_min=10, tm_s 阅读全文
posted @ 2017-08-04 15:48 Sawyer Ford 阅读(223) 评论(0) 推荐(0)
摘要:os是个常用的模块,必须熟练。 参考资料: https://docs.python.org/3/library/os.html How do I remove/delete a folder that is not empty with Python? 阅读全文
posted @ 2017-07-31 20:46 Sawyer Ford 阅读(163) 评论(0) 推荐(0)
摘要:本文主要讲述如何使用Python发送protobuf数据。 安装protobuf 安装成功。 python安装protobuf 环境搭建完毕,下面给个demo。 创建my_bidding.proto 编译proto文件 编译成功,会产生my_bidding_pb2.py。 编写Python脚本 阅读全文
posted @ 2017-07-26 17:13 Sawyer Ford 阅读(3437) 评论(0) 推荐(0)
摘要:Python: 简明 Python 教程 廖雪峰Python3教程 Python快速教程 (手册) 爬虫: 汪海的实验室:Python爬虫入门教程 静觅: Python爬虫学习系列教程 Flask: Flask官方文档 Flask之旅 书籍: 《简明Python教程》 《Python核心编程》第二版 阅读全文
posted @ 2017-05-13 09:56 Sawyer Ford 阅读(185) 评论(0) 推荐(0)
摘要:re是一个使用频率很高的模块。 这个例子想说明的是,Python中的字符串如果使用'r'前缀,字符串中的内容就是本身,没有转义。 re模块的常用函数: 一个常用的flag是: 如果一个正则表达式需要多次使用,可以预编译该表达式。 最后要说的是贪婪匹配和非贪婪匹配,看个例子: 输出: 默认是贪婪匹配, 阅读全文
posted @ 2017-05-11 21:05 Sawyer Ford 阅读(215) 评论(0) 推荐(0)
摘要:MAC版本:EI Captain Python版本: 2.7.10 MAC默认没有安装pip,所以首先 然后安装Scrapy: 安装过程中报错: "libxml/xmlversion.h' file not found",解决方案如下: 再次安装,成功。 然而,后续执行scrapy startpro 阅读全文
posted @ 2017-04-21 15:19 Sawyer Ford 阅读(575) 评论(0) 推荐(0)
摘要:对于Python2,首先安装驱动MySQL-python 执行命令后提示:"EnvironmentError: mysql_config not found"。 我是通过lnmp一键安装包安装的MySQL,文件位置不对,所以需要加一句: 再次执行安装命令,又报了一个错: "Python.h:没有那个 阅读全文
posted @ 2017-04-13 22:56 Sawyer Ford 阅读(224) 评论(0) 推荐(0)
摘要:直接上代码: 参考资料: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432005226355aadb8d4b2f3f42f6b1d6f2c5bd8d5263000 阅读全文
posted @ 2017-04-10 20:33 Sawyer Ford 阅读(483) 评论(0) 推荐(0)