python 07_day

1.文件操作:

f=open(...)

是由操作系统打开文件,那么如果我们没有为open指定编码,那么打开文件的默认编码很明显是操作系统说了算了

操作系统会用自己的默认编码去打开文件,在windows下是gbk,在linux下是utf-8#这就用到了上节课讲的字符编码的知识:若要保证不乱码,文件以什么方式存的,就要以什么方式打开。
f=open('a.txt','r',encoding='utf-8')
#1. 打开文件的模式有(默认为文本模式):
r ,只读模式【默认模式,文件必须存在,不存在则抛出异常】
w,只写模式【不可读;不存在则创建;存在则清空内容】
a, 只追加写模式【不可读;不存在则创建;存在则只追加内容】
 
#2. 对于非文本文件,我们只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码、图片文件的jgp格式、视频文件的avi格式)
rb
wb
ab
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码
 
#3,‘+’模式(就是增加了一个功能)
r+, 读写【可读,可写】
w+,写读【可写,可读】
a+, 写读【可写,可读】
 
#4,以bytes类型操作的读写,写读,写读模式
r+b, 读写【可读,可写】
w+b,写读【可写,可读】
a+b, 写读【可写,可读】
read(3):

  1. 文件打开方式为文本模式时,代表读取3个字符

  2. 文件打开方式为b模式时,代表读取3个字节

其余的文件内光标移动都是以字节为单位的如:seek,tell,truncate

注意:

  1. seek有三种移动方式0,1,2,其中1和2必须在b模式下进行,但无论哪种模式,都是以bytes为单位移动的

  2. truncate是截断文件,所以文件的打开方式必须可写,但是不能用w或w+等方式打开,因为那样直接清空文件了,所以truncate要在r+或a或a+等模式下测试效果。
文件的数据是存放于硬盘上的,因而只存在覆盖、不存在修改这么一说,我们平时看到的修改文件,都是模拟出来的效果,具体的说有两种实现方式:

方式一:将硬盘存放的该文件的内容全部加载到内存,在内存中是可以修改的,修改完毕后,再由内存覆盖到硬盘(word,vim,nodpad++等编辑器)


复制代码
import os  # 调用系统模块

with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:
    data=read_f.read() #全部读入内存,如果文件很大,会很卡
    data=data.replace('alex','SB') #在内存中完成修改

    write_f.write(data) #一次性写入新文件

os.remove('a.txt')  #删除原文件
os.rename('.a.txt.swap','a.txt')   #将新建的文件重命名为原文件
复制代码
import os

with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:
    for line in read_f:
        line=line.replace('alex','SB')
        write_f.write(line)

os.remove('a.txt')
os.rename('.a.txt.swap','a.txt') 
复制代码
 
 
方式二:将硬盘存放的该文件的内容一行一行地读入内存,修改完毕就写入新文件,最后用新文件覆盖源文件


复制代码
import os

with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:
    for line in read_f:
        line=line.replace('alex','SB')
        write_f.write(line)

os.remove('a.txt')
os.rename('.a.txt.swap','a.txt')

2.数据类型的转换!

 
 
数自转bool
N = bool(1)
True
N = bool(-1)
true
N = bool(0)
flase
n = int(true)
1
n = int(flase)
0
字符串转bool
字符串只要不是空的,其bool都是True
s = "zhong"
s1 = bool(s)
print(s1)
True
其他类型转bool
列表,元组,集合,字典,等只要不是空的转为字符串都是True
字符串转其他类型
字符串转列表
s = "zhong"
s1 = list(s)
print(s1)
['z', 'h', 'o', 'n', 'g']
s = "zhong xxx"x= s.split(" ")
print(x)
['zhong', 'xxx']
字符串转元组
s = "zhong"
s1 = tuple(s)
print(s1)
('z', 'h', 'o', 'n', 'g')
字符串转字典
di = { }
s = "zhong"for i in s:
di.setdefault(i)
print(di)
{'z': None, 'h': None, 'o': None, 'n': None, 'g': None}
字符串转集合
s = "zhong"
s1 = set(s)
print(s1)
{'g', 'n', 'h', 'o', 'z'}
字符串转int
s = "2"s1 = int(s)
print(s1)
2
字典转其他类型
字典转字符串
di = {"中国":"北京","河南":"郑州"}
s1 = str(di)
print(type(s1))
<class 'str'>
字典转列表
di = {'中国':'北京','河南':'郑州'}
x = list(di)
print(x)
['中国', '河南']
字典转集合
di = {'中国':'北京','河南':'郑州'}
x = set(di)
print(x)
{'中国', '河南'}
字典转元组
di = {'中国':'北京','河南':'郑州'}
x = tuple(di)
print(x)
{'中国', '河南'}
列表转其他类型
列表转字典
di = ['中国','北京','河南','郑州']
x = dict(di)
print(x)
{'': '', '': '', '': '', '': ''}
##报错!!!!!!
di = ['','','','','河南','郑州']
x = dict(di)
print(x)
di = ['中国北京','河南郑州']
x = dict(di)
print(x)
列表转字符串
di = ['中国','北京','河南','郑州']
x = "".join(di)
print(x)
中国北京河南郑州
列表转元组
di = ['中国','北京','河南','郑州']
x = tuple(di)
print(x)
('中国', '北京', '河南', '郑州')
列表转集合
di = ['中国','北京','河南','郑州']
x = set(di)
print(x)
{'北京', '中国', '河南', '郑州'}
元组转其他类型
元组转字符串
di = ('中国','北京','河南','郑州')
x = str(di)
x = str(di)
print(x)
<class 'str'>
('中国', '北京', '河南', '郑州')
元组转列表
di = ('中国','北京','河南','郑州')
x = list(di)
print(type(x))
print(x)
<class 'list'>
['中国', '北京', '河南', '郑州']
元组转字典
di = ('中国','北京','河南','郑州')
x = dict(di)
print(type(x))
print(x)
<class 'dict'>
{'': '', '': '', '': '', '': ''}
###报错####
di = ('中国北京','河南','郑州')
x = dict(di)
print(type(x))
print(x)
Traceback (most recent call last):
File "D:/PythonCode/day7/tsxt.py", line 2, in <module>
x = dict(di)
ValueError: dictionary update sequence element #0 has length 4; 2 is required
元组转集合
di = ('中国北京','河南','郑州')
x = set(di)
print(type(x))
print(x)
<class 'set'>
{'河南', '中国北京', '郑州'}
集合转其他类型
集合转字典
di = {'中国','北京','河南','郑州'}
x = dict(di)
print(type(x))
print(x)
<class 'dict'>
{'': '', '': '', '': '', '': ''}
###报错####
di = {'中国北京','河南','郑州'}
x = dict(di)
print(type(x))
print(x)
Traceback (most recent call last):
File "D:/PythonCode/day7/tsxt.py", line 2, in <module>
x = dict(di)
ValueError: dictionary update sequence element #2 has length 4; 2 is required
集合转列表
di = {'中国北京','河南','郑州'}
x = list(di)
print(type(x))
print(x)
<class 'list'>
['河南', '中国北京', '郑州']
集合转字符串
di = {'中国北京','河南','郑州'}
x = str(di)
print(type(x))
print(x)
<class 'str'>
{'郑州', '中国北京', '河南'}
集合转元组
di = {'中国北京','河南','郑州'}
x = tuple(di)
print(type(x))
print(x)
<class 'tuple'>
('郑州', '河南', '中国北京')
 
  
 
 

猜你喜欢

转载自www.cnblogs.com/changtao/p/9879188.html