windows的换行符:\n\r

linux的换行符:\n

文件的数据处理:

r 以只读模式打开文件(默认模式)
w 以只写模式打开文件
a 以追加模式打开文件

r+b 以读写模式打开文件(以读/写方式打开一个已存在的文件)
w+b 以写读模式打开文件(以读/写方式建立一个新的文本文件,如果文件存在,w+会自动把已有文件的内容清空。)
a+b 以追加及读模式打开文件

r、r+把字符指针放到开头

b:以二进制形式打开文件,解决了windows和linux换行符不一致的问题

f = file('/etc/passwd.bak','r') 等效于 f = file('/etc/passwd.bak')

f = file('/etc/passwd.bak','r')
for line in f.readlines():
line = line.strip('\n').split(':')
print line

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2QAAABxCAYAAAC6G2B9AAAgAElEQVR4nO2dv3KjPBeHj795LyXOTDK+AnIF9jap3G5HStNslzLdNqS0u21dpQm+gnAFHhdL7mQLPiQwBiyBJCPzx79nxvO+68jiHB0hdNDREf379y8ufg6HQywmiF1yYj+S/dmNiSh2g+pvKKbyl3r1Gpdtj8h3uG5O9cKRHzvkxiLtfKeqd9YWjn9Wrlovv16lHG9fx+F1lKrlMlTaRKdsJteZbryaig7895U6ud2d2HGqtgfg9uD3s+tKx4UytsYz1XqzMYl/JOW1xpLK73i9Ku0AAAAA3DbUmkOWlzk+4EnsxBjV2y3cQUpmI9zJKulXJ7d6W5zVW3XGeHVu9n21XoEMOmVlMkgd6Wqd6YQr/z28MnDLHB0RyX1wfLkj+zSPl9ILn403xY/stuQvXWRjzlEfzbHkyPlLKQAAAACImDAnjApEUUQPDw90zo5eJm/0GH3R6k7wZ2Ns1Tsydi80eXuk+GvVblkAAJBhOpZ8v9PTdEtLjOsAAABAI//TKx6SN53QZDKhp/fviy78/f7E65lMFrS5qCYAAAB94vtzS6GzhDMGAAAAKPCfetE5reOY1i1d+G71RTEWcAAAYGTs6LcXkht8dS0IAAAAMAg0V8hAF+xeJjRZbIhCL1tVfCLZAqVOWQAAkKE1lrAQxcmkFPWwWST///R+TZEBAACAQaKxhwwAAAAAAAAAQJ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j1k0GS+qhkbn0mvCkO3yvpOwgcQBAl0zYqljxiyiK6OHhoSt5eksee99GIoaeAd2GyZh1swXaDBRBfwDtke57rh7GDQAAKsAhAwAAAADQgSehKa81uUF8WyuCAIDWgEMGAAAAAAAAAB3RSdp7AAAAAAAAAABE/wf43JubQhfnZgAAAABJRU5ErkJggg==" alt="" />f.readlines生成的是一个列表,f.read

f = file('/etc/passwd.bak','r')
for line in f.readlines():
line = line.strip('\n').split(':')[0]
print line

常见文件方法:

readlines、xreadlines、next、f.read、close、closed、seek、tell、write、writelines、mode、name 、flush、closed、truncate

>>> f = file('/etc/passwd.bak','r')
>>> f.name
'/etc/passwd.bak'

read生成的是一个字符串,readlines生成的是一个列表

>>> f.read()
'root:x:0:0:root:/root:/bin/bash\nbin:x:1:1:bin:/bin:/sbin/nologin\ndaemon:x:2:2:daemon:/sbin:/sbin/nologin\nadm:x:3:4:adm:/var/adm:/sbin/nologin\nlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin\nsync:x:5:0:sync:/sbin:/bin/sync\nshutdown:x:6:0:shutdown:/sbin:/sbin/shutdown\nhalt:x:7:0:halt:/sbin:/sbin/halt\nmail:x:8:12:mail:/var/spool/mail:/sbin/nologin\nuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin\noperator:x:11:0:operator:/root:/sbin/nologin\ngames:x:12:100:games:/usr/games:/sbin/nologin\ngopher:x:13:30:gopher:/var/gopher:/sbin/nologin\nftp:x:14:50:FTP User:/var/ftp:/sbin/nologin\nnobody:x:99:99:Nobody:/:/sbin/nologin\ndbus:x:81:81:System message bus:/:/sbin/nologin\nvcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin\nsaslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin\npostfix:x:89:89::/var/spool/postfix:/sbin/nologin\nsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin\n'

seek:改变文件内字符指针的位置,tell告诉文件内字符指针的位置

>>> f.seek(0)
>>> f.tell()
0L
>>> f.seek(300)
>>> f.tell() 300L
>>> f.seek(61)
>>> f.readline()
'gin\n'

检查文件是否关闭

>>> f.closed
False

truncate:从文件开头开始截取n个字节

>>> f = file('/etc/passwd.bak','r+')
>>> f.truncate(5)
>>> f.close()
[root@hy tmp]# more /etc/passwd.bak
root:

xreadlines:写进内存一行,读一行,使用大文件,readlines将文件全部写入内存然后读文件

字符串处理

find  查找字符串
index   字符串索引值
capitalize   首字母大写
lower  转小写
upper  转大写
swapcase 大小写转换
split  string转list,按指定符号分割
join   list转string,按指定符号连接
len  字符串长度
cmp  比较首字符对应的ASCII大小,数字直接比较大小
max   ASCII最大的字符
min   ASCII最小的字符
startwith 以某些字符开头

>>> msg = "what's your company's name?"
>>> msg.find('name')
22

>>> msg.index('name')
22

>>> msg.capitalize()
"What's your company's name?"

>>> msg.split()
["what's", 'your', "company's", 'name?']
>>> msg.split("'")
['what', 's your company', 's name?']

>>> msg_list = msg.split("'")
>>> msg_list
['what', 's your company', 's name?']
>>> '|'.join(msg_list)
'what|s your company|s name?'

>>> len(msg)
27

>>> x,y=1,2
>>> cmp(x,y)
-1
>>> cmp(y,x)
1
>>> x,y='A2','a1'
>>> cmp(x,y)
-1

>>> x='12aA'
>>> min(x)
'1'
>>> max(x)
'a'

>>> x.startswith('1')
True
>>> x.startswith('2')
False

list VS dict

dict:查找和插入速度快,但耗内存,无序

list:查找和插入的时间随着元素的增加而增加,浪费内存少,有序

set:无序,元素不重复,作用:关系测试;去重

>>> x={1,2,3,4}
>>> y={3,4,5,6}
>>> x&y
set([3, 4])
>>> x|y
set([1, 2, 3, 4, 5, 6])
>>> x^y
set([1, 2, 5, 6])
>>> x-y
set([1, 2])

>>> z={1,2,4}
>>> z.issubset(x)
True
>>> x.issuperset(z)
True

Python之路【第二篇】:Python基础(二)的更多相关文章

  1. python之路第二篇(基础篇)

    入门知识: 一.关于作用域: 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 10 == 10: name = 'allen' print name 以下结论对吗? ...

  2. Python之路(第二篇):Python基本数据类型字符串(一)

    一.基础 1.编码 UTF-8:中文占3个字节 GBK:中文占2个字节 Unicode.UTF-8.GBK三者关系 ascii码是只能表示英文字符,用8个字节表示英文,unicode是统一码,世界通用 ...

  3. Python之路第二天,基础(2)-基本数据类型

    一.Python数据类型 数 在Python中有4种类型的数,整数,长整数,浮点数和复数. 2是一个整数的例子 长整数不过是大一点的整数 3.23和52.3E是浮点数的例子.E标记表示10的幂.52. ...

  4. Python之路(第九篇)Python文件操作

    一.文件的操作 文件句柄 = open('文件路径+文件名', '模式') 例子 f = open("test.txt","r",encoding = “utf ...

  5. Python开发【第二篇】:初识Python

    Python开发[第二篇]:初识Python   Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏 ...

  6. 第二篇 python进阶

    目录 第二篇 python进阶 一 数字类型内置方法 二 字符串类型内置方法 三 列表类型内置方法(list) 四 元组类型内置方法(tuple) 五 字典内置方法 六 集合类型内置方法(self) ...

  7. [转帖]虚拟内存探究 -- 第二篇:Python 字节

    虚拟内存探究 -- 第二篇:Python 字节 http://blog.coderhuo.tech/2017/10/15/Virtual_Memory_python_bytes/ 是真看不懂哦     ...

  8. 前端第二篇---前端基础之CSS

    前端第二篇---前端基础之CSS 目录 一.css介绍 二.css语法 三.css的几种引入方式 四.css选择器 五.css属性相关 六.盒子模型 拓展 一.css介绍 CSS(Cascading  ...

  9. 我的Python成长之路---第二天---Python基础(7)---2016年1月9日(晴)

    再说字符串 一.字符串的编码 字符串的编码是个很令人头疼的问题,由于计算机是美国人发明的,他们很理所当然的认为计算机只要能处理127个字母和一些符号就够用了,所以规定了一个字符占用8个比特(bit)也 ...

  10. 初学Python——文件操作第二篇

    前言:为什么需要第二篇文件操作?因为第一篇的知识根本不足以支撑基本的需求.下面来一一分析. 一.Python文件操作的特点 首先来类比一下,作为高级编程语言的始祖,C语言如何对文件进行操作? 字符(串 ...

随机推荐

  1. myeclipse中的类恢复之前的版本方法

    1.右键要恢复的文件,点击如下的选项. 2.界面中出现之前保存的版本,双击要查看的版本,可对比版本之间的不同之处.点击Replace,恢复版本.

  2. 数组按时间(字符串->Date)排序

    不说了 ,直接上代码.Talk is cheap , show me the code. NSArray *stortedArray = [wkSelf.dataArray sortedArrayUs ...

  3. wireshark 包过滤

    tcp.port == 443 or udp.port==443 or tcp.port==53 or udp.port==53

  4. 还原openstack配置文件的方法

    cp -a /etc/neutron/neutron.conf /etc/neutron/neutron.conf.bakcp -a /etc/neutron/plugins/ml2/ml2_conf ...

  5. JS获取网页中HTML元素的几种方法分析

    getElementById getElementsByName getElementsByTagName 大概介绍 getElementById ,getElementsByName ,getEle ...

  6. Chapter 2 Open Book——18

    "Wow," Mike said. "It's snowing."I looked at the little cotton fluffs that were ...

  7. Chapter 1 First Sight——32

    The class seemed to drag on longer than the others. 这堂课看起来比别的课要长. Was it because the day was finally ...

  8. 第一次使用unity3d

    今天暂且做个记录,因为第一使用了unity3d,进行了很长时间的安装和调试,进行了简单的使用,能简单的在页面上面建立了一个方块和一个球. 简单了解了unity中的一些基本概念.总结一下,一个物体可以有 ...

  9. DOM操作-动态创建网页元素

    动态创建新的DOM元素,是JavaScript操作网页对象模型的重要手段之一 代码: <!DOCTYPE html> <html> <head> <title ...

  10. mysql 数据表

    CREATE DATABASE IF NOT EXISTS  `shop`;USE `shop`; drop table if exists lidepeng; create table lidepe ...