python 文件的操作
前言 记性越来越不好了,每次用 python 读写文件都会和 golang 搞混,今天好好抽个时间单独来复习一下,记录下来。 常用的文件函数:open() read() readline() readlines() write() writelines() open() open() 函数用来打开文件,定义 ... 阅读更多
前言 记性越来越不好了,每次用 python 读写文件都会和 golang 搞混,今天好好抽个时间单独来复习一下,记录下来。 常用的文件函数:open() read() readline() readlines() write() writelines() open() open() 函数用来打开文件,定义 ... 阅读更多
一、添加 1.1 append 在末尾添加元素
|
1 2 |
stu1 = ["aaa", "bbb", "ccc"] stu1.append("ddd") # ["aaa", "bbb", "ccc", "ddd"] |
1.2 insert 插入至指定位置
|
1 2 |
stu1.insert(1, "eee") print stu1 # ["aaa", "eee", "bbb", "ccc", "ddd"] |
1.3 extend 添加列表 [crayon-69646206279774178 ... 阅读更多
一、现象 当列表中存在中文时,输出列表将会产生乱码:
|
1 2 3 |
>>> stus = ["小明", "小李", "小花"] >>> print stus ["xd0xa1xc3xf7", "xd0xa1xc0xee", "xd0xa1xbbxa8"] |
二、解决方法 2.1 方法一 使用 decode("string_escape") 解决:
|
1 2 |
>>> print str(stus).decode("string_escape") ["小明", "小李", "小花"] |
2.2 方法二 通过字符串 ... 阅读更多
一、字符串查找 1.1 str.find(sub, start=None, end=None) 在字符串中查找相应的字符串或字符,找到返回下标,否则返回-1 可以设置 start 和 end 在指定下标范围内查找,默认为 None 表示从字符串的开头到结尾。 [crayon-6964620627d8867 ... 阅读更多
三月份开始接触 python,到现在差不多半年时间,中途两个月完全没碰,到现在再次用到竟然有点生疏了。当时学的时候也是囫囵吞枣,看了一点语法就开始做项目,基础也不是很扎实,所以决定从今天开始重新学习 python,从最基本的开始,一点一滴来积累。 python 中的字符串和 int 类型互转可以说是所有语言里 ... 阅读更多
python 中的 「==」 用来判断两个变量的值是否相等,如:
|
1 2 3 |
a = [11, 22, 33] b = [11, 22, 33] print a == b //True |
is 也是用来判断是否相等,但是是判断地址:
|
1 2 3 4 5 6 7 |
a = [11, 22, 33] b = [11, 22, 33] c = a print c == b //True print c is b //False print c is a //True |
因为 a 和 b 是两块不同的地址空间,虽然值相同,但是地址不同,所以使 ... 阅读更多
概述 刚装系统后,电脑开机飞快,然后装驱动、装软件后就发现每次开机都要先黑屏个一到两分钟。 最开始一直找不到原因,总以为是某个流氓软件,然后就各种关闭启动项卸载软件,最后发现并没有用。 一直持续到某一天偶然卸载了显卡驱动,才发现竟然是显!卡!的!锅! 百度了一下造成这种黑屏现象的原因是显卡的 ULPS ... 阅读更多
把 CentOS6.5 默认的 python2.6 升级到了 python2.7,然后运行 yum 命令的时候就出现了错误:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
There was a problem importing one of the Python modules required to run yum. The error leading to this problem was: No module named yum Please install a package which provides this module, or verify that the module is installed correctly. It"s possible that the above module doesn"t match the current version of Python, which is: 2.7.13 (default, Aug 18 2017, 21:52:09) [GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] If you cannot solve this problem yourself, please go to the yum faq at: https://yum.baseurl.org/wiki/Faq |
原因是因为 yum 是用 python 写的,并不兼容 2.7,所以运行会报错。解决办法是修改 yum 源文件,指定 python 版本为老 ... 阅读更多
一、说明 CentOS6.5 自带 python 环境为 2.6,公司的 python 环境为 2.7. 为了避免出现以后代码出现版本差异,所以把自带的 2 .6 版本升级到了 2.7,过程十分曲折。。。。 中途遇到的问题和解决方法请点击:Python 安装时遇到的问题 二、安装步骤 1 、下载安装包 官方下载地址为:ht ... 阅读更多
repr() 函数的功能是返回对象的规范字符串表达式,什么叫规范表达式呢???首先我们看看函数的声明:
|
1 2 3 4 5 6 7 8 |
def repr(p_object): # real signature unknown; restored from __doc__ """ repr(object) -> string Return the canonical string representation of the object. For most object types, eval(repr(object)) == object. """ return "" |
好像并没有发现什么有用的信息,那就先看看 str() 函数吧。 str() 函数的功能则是将对象转换成一个字符串,准确的来说 str() 并不是 ... 阅读更多