File Operations
Read-only
Using open () to open a file, to obtain the file handle, and can be operated by a file handle
Open the corresponding difference will be capable of performing different operations
Open file:
r,w,a r+,w+,a+ rb,wb,ab
r read-only operation mode can only be read
f=open('文件名称',mode='操作方式',encoding='编码类型')
f.read() 读取
f.close() 关闭文件
f.readline()读行
f.redlines()逐行读取
open into the first parameter is the name of the file to open the second parameter is what kind of operation to be performed on this file, the third parameter is used to buy content Who told you to open the file encoding summarized.
f can be written in any variable, is called a file handle, operator files, documents or other objects operation, the operating system is the Open function call python, windows default encoding is gbk, Linux default encoding is utf-8, so the file what encoding saved, opened in what ways, mode is open, a common r, w, a, r +, a +, w +, rb, wb, ab and other default does not write is r, the process is to open the file, generate a file handle, a file handle corresponding to the operation, close the file.
rb mode: Read Only Byte
f = open ( 'filename', mode = 'rb) # Note rb, wb, ab, can not write encoding mode
s=f.read()
print(s)
The resulting bytes are byte type
rb role: read text document of the time, such as reading images, MP3 and video, you will need to use rb, because this data is no way to directly write out
For their transport and storage.
Relative and absolute paths
Relative path: that is, where to find the same level, at the same folder, the file name can be written directly
Absolute path: the path to find the file on computer disk, then the equivalent of do not know where to find somewhere else to go, has been to find the file name
Escape
Sometimes when you enter the path
C:\user\ner #这个\n会成为换行符,防止被转义就需要
#两种方法可以防止转义
1.C:\\user\\ner #双斜杠
2.r'C:\user\ner #前面加r
Write covered w
To write files when finished will form good habits refresh flush ()
f=open("文件",mode='w',encoding='utf-8')
f.write("内容")
f.flush()
f.close()
When using w mode, the file will open the file to empty and then write. Note that in covering the time of writing, the file will open in the empty file,
w reading operation can not be performed
wb mode, edit the file can not be opened, but the time to write the file must be converted into a string of bytes of data utf-8
f = open ( "file", mode = 'wb') attention must not write encoding
msg = 'Hello' .encode (utf-8)
f.write(msg)
f.flush () # refresh
f.close()
Add a, ab, a +
Files are added at the end, added, regardless of the cursor position
a, w mode if the file does not exist, creates a new file
f=open('内容',mode='a',encoding='utf-8')
msg = f.write ( "after this saccharification")
Write mode r +
Read and write the name suggests, it must be read before write, because the default cursor is at the front, when finished it, the cursor is read to the end, and then write
r + mode
f=open("文件名称",mode='r+',encoding='utf-8')
s=f.read()
f.write("针棒")
f.flush()
f.close()
print(s)
#正常的读取,写在结尾
R + pit pattern
After the write operation is performed, if the read contents no matter how many, how much the cursor is displayed, then the write operation when the file or are at the end of the operation.
Read-write mode W +
f = open ( "filename", mode = 'w +', encoding = 'utf-8')
First empty the contents, and then written, then read, but to read the content is empty, not used
f=open("文件名",mode='w+',encoding='utf-8')
f.write("xiaogui")
s=f.read()
f.flush()
f.close()
print(s)
读取不到内容,和覆盖写一样,都会清空再写内容
Additional read a +
f=open("文件名",mode='a+',encoding='utf-8')
f.write('内容')
f.flush()
s=f.read()
f.close()
print(s)
a+模式下,无论是先读还是后读都是读不到数据的
Other operations
seek () is the position of the cursor movement, the mobile unit is byte byte, if all the Chinese portion utf-8 multiples of three, if
It is usually seek to move to the beginning of the end, waving
Move to the beginning seek (0,0)
And moving to the seek (0,2)
Moved to the current position seek (0,1)
Moving a word: seek (3) to move the cursor is moved in bytes
f = open("小娃娃", mode="r+", encoding="utf-8")
f.seek(0,0)将光标移动到开头
s=f.read() 读取内容,此时光标读取完以后移动到结尾
print(s)
f.seek(0) 再次移动到开头
f.seek(0,2) 移动光标到结尾
s1=f.read() 因为光标移动到结尾 所以什么也没读取到
print(s1)
f.seek(0) 将光标移动到开头
f.write("战鼓啊")写入信息,现在光标在9 因为中文的utf-8编码一个中文三字节,3*3=9
f.flush()
f.close()
f.tell() 查看光标位置
tell View the cursor position:
f = open("小娃娃", mode="r+", encoding="utf-8")
f.seek(0)移动光标到开头
s=f.read() 读取内容,光标移动到结尾
print(s)
f.seek(0) 开头
f.write("美滋滋") 三个字 9个字节
f.tell() 查看光标位置 9
Modify the file
File modification, only the contents of the file is read into memory, will delete the source file information after the modification is completed, the name of the new file name into the old file
- Malpractice operation, once the entire contents of the line read, memory overflow
import os
with open("../path1/小娃娃", mode="r", encoding="utf-8") as f1,\ #读
open("../path1/小娃娃_new", mode="w", encoding="UTF-8") as f2: #写
s=f1.read()
new_s=s.replace("冰糖葫芦","大白梨")
f2.write(new_s)
os.remove('文件路径') #删除原文件
os.remove("新文件笔记","旧文件名") #重命名新文件
- Solution: read line by line and operations
import os
with open("../path1/小娃娃", mode="r", encoding="utf-8") as f1,\ #旧 读
open("../path1/小娃娃_new", mode="w", encoding="UTF-8") as f2: #新 改
for line in f1:
new_line=line.replace("大白梨","冰糖葫芦")
f2.write(new_line)
os.remove("小娃娃") #删除源文件
os.remove("新文件","源文件") #重命名新文件
- Read Line f.readline () to read a line wrap
- print (f.readline () .strip ()) read multiple lines to remove line breaks
- print (f.readslines ()) is read line by line, stored in the list
- Use for line by line acquisition cycle
The initial function
function
用函数来实现len
s=[1,2,3,4,5]
count=0
for i in s:
count+=1
print(count)
1, the function definition: A reusable, to achieve a certain function code segments together tissue
2, the function syntax:
DEF function name (parameter):
function body
return
2.1, the function name is a variable, so named rule requires follow variable naming rules
3, function call
function name ()
4, the function return value
return may be empty, None, a single, a plurality of words in a tuple and returns to the caller
5, parameters of the function:
call type declaration function variables defined reference
1, location parameter
2, the default parameters
passed to the function when the function call is called argument
1, the positional parameters
define how many, when you call must also pass the number
2, keyword arguments
3, mixing parameters
Call functions
The function name in parentheses can call the function when the function name written in parentheses, the body of the function will be executed
def len():
s=[1,2,3,4,5]
count=0
for i in s:
count+=1
print(count)
len() #函数的调用
Function's return value
def func():
print("1")
func()
func()
func() #多次调用
This function can be called multiple times
Return function's return value
def func():
print(1)
return
func()
return 吧这个结果返回给了调用者 因为return后面没有跟东西 所以默认返回None,并且return以后后续代码不再执行,函数的返回值有多个结果的时候就是返回一个元祖,调用者也可以通过解构获得多个变量,返回给函数的调用者,没有return也是默认返回None
Summary return
- Experience return, the end of this function, return code will not execute
- The default is to return return None, behind a plurality of return value is a tuple of the form
Location parameters required before the keyword arguments
Function parameters
参数是函数括号里的内容,函数在调用的时候回指定一个具体的变量的值,就是参数 def 函数名(参数): 函数体 def func(chat): #形参 print('打开+chat') func(chat) #实参 在调用的时候给chat一个值再执行函数体
2. Types of actual parameter
- Parameter, written in the position of cross-function declaration parameter, complete the form, indicating that the function requires xxx
- Arguments: Write when you call the function to transfer the value of the function, followed by the arguments, when in actual execution to the information transfer function, expressed to the function xxx
- Parameter passing: from call functions defined function values to speak of a process called mass participation
3. classification parameters
def func(a,b,c=1): #形参 a,b是关键字参数,c是默认参数 排序就是位置>关键字>默认 prin(a) print(b) print(c) #形参就是变量名 实参就是指 传参就是把值赋值给形参 func(a=2,b=3) #实参
def func(a,b): print(a+b) s=func(1,2) print(s)
- Summary parameters: positional parameters, keyword parameters, the positional parameters parameter must be mixed in front of the keyword arguments
- Location parameters, in accordance with the position assignment
- Keyword arguments, to find the parameters by keywords
- Default parameters: default values of the parameters have default values commonly used, the following arguments to the default value if the parameter assignment, it will change to the assigned value of argument
Intrinsically for a while loop;
s=[1,2,3,4,5]
n=s.__iter__() #迭代器
while True:
try:
print(n.__next__())
except StopIteration:
break
Dynamic parameters, namespace, nested functions
The dynamic parameters of the function
- * Args is a universal function of position, can be received at any of a plurality of variable args, program args ape Unified defined function with "*" is called polymerization in the position indicated by * parameter takes any parameter, default parameter values placed behind the dynamic position parameter, this does not override the default value of the parameter
- The order is
- Sequence positions: position parameter> dynamic position parameter>
- def song (* args) the inside * \ is called the polymerization, the obtained polymerization result is a tuple
- print (a, b, * args) This is called the fight inside the break, break up the individual elements can be obtained.
- When receiving dynamic parameters Note: in later dynamic parameters must position parameters
- Order parameter: the position parameter> dynamic parameter> default parameters
3. ** kwargs obtained dict is a dictionary of keyword arguments accepted * kwargs this star can take this dictionary key ** kwargs universal mass participation (dynamic keyword parameters)
Priority parameters: location parameter> dynamic positional parameters args> default parameter> kwargs dynamic keyword arguments
- Universal parameter passing: dynamic positional parameters, dynamic keyword arguments
- def song(*args,**kwargs):
- Position parameter> dynamic position parameters
Notes functions
def func(a,b):
'''
逻辑判断...
:param a:str
:param b:int
return:bool
'''
print(a,b)
def func(user,password):
'''
密码加密
:param user:用户名 str
:param password:密码 str
:return:加密的密码 MDS
'''
print(user,password)
print(func.__doc__)
print(func2.__doc__)
print(func.__name__)
Namespaces
print(b)
a=10
def func():
b=20
print(b)
内置空间:print input len 这是python自带的内置空间
全局空间:当前py文件需要开辟的孔家存放在全局空间
局部空间:函数中开辟的空间都是局部空间
加载顺序:
内置空间>全局空间>局部空间
取值顺序:
局部空间>全局空间>内置空间(还找不到就会犯错)
作用域:
全局作用域:内置空间+全局空间
局部作用域:局部空间
Nested functions
def song():
print(1)
def fan():
print(2)
return fan()
song()
1,2
____________________
def song():
a=1
def jia():
b=2
print(b)
print(a)
def fan():
pring(b)
return fan()
return jia
song()
2 1 2
____________
def func():
a=1
foo()
pring(a)
def foo():
b=2
print(b)
func()
def a():
a=1
c=()
print(c)
def b():
b=2
print(b)
def c():
c=3
print(a)
def run():
a()
run()
____________________
def func():
a=1
def b():
print(a)
def foo():
b=1
def z():
print(func)
print(b)
ret=z()
func()
return ret
def run():
foo()
print(run())
func 的内存地址 1 还有None
________________________
def func(a):
foo(a)
def foo(e):
b(e)
def b(c):
print(c)
func(15)
15
______________________
a=10
def func():
a=10+1
print(a)
func()
11 别被迷惑
________________
def f1():
a=10
print(a)
def f2():
a=15
print(a)
def f3():
global a
a += 1
print(a)
print(a)
f3()
print(a)
f2()
print(a)
f1()
10 15 11 15 10 10
__________________
a=10
def f1():
a=10
def f2():
a=15
def f3():
global a
a += 1
print(a)
print(a)
f3()
print(a)
f2()
print(a)
f1()
10 10 15 11
—————————————————————
a=100
def func():
global a
a=28
print(a)
func()
print(a)
100 28
____________
a=10
def func1():
a=20
def func2():
nonlocal a
a=30
print(a)
func2()
print(a)
func1()
30 30
___________________
a=1
def fun_1():
a=2
def fun_2():
nonlocal a
a=3
def fun_3():
a=4
print(a)
print(a)
fun_3
print(a)
print(a)
fun_2()
print(a)
print(a)
fun_1()
print(a)
1 2 3 3 3 1
#注意看这个没有被调用的函数
_______________________
global nonlocal
global: modifying the global space corresponding to the value of variable
nonlocal: Modify function in outer space in a local variable values, do not involve the global space, only modification to the previous layer closest to him, the most recent one did not continue to go up, know where to find the most outer function
Ternary operator
Ternary operator: variable condition is satisfied the determination condition is not satisfied condition results Results
return a if a>b else b
a = 10
b = 20
c = a if a> b else b
变量名 = 条件成立的结果 条件 条件不成立的结果
enumerate enumeration
Automatic sorting
enumerate: enumeration for (Iterable) objects (such as a list, string) an iteration / traversable, which is composed of the enumerate a sequence index, which can be obtained by using the index value and at the same time.
enumerate (iterables, 1) determined from a few counting
li = ['alex','银角','女神','egon','太白']
for i in enumerate(li):
print(i)
(1, 'alex')
(2, '引脚')
(3, '女神')
(4, '太白')
for index,name in enumerate(li,1):
print(index,name)
1 alex
2 引脚
3 女神
4 太白
for index, name in enumerate(li, 100): # 起始位置默认是0,可更改
print(index, name)
100 alex
101 引脚
102 女神
103 太白
The first class using the object name of the function, f formatting recursive iterator
The function name and the use of first-class objects
First-class objects particularity:
It can be used as the value assigned to the variable
def func():
print("呵呵")
print(func)
a=func
a() #函数调用 func()
a=func 那么 a+()就可以调用这个函数,吧这个函数赋值给一个变量,
As elements stored in the container
def func():
print(func)
func()
lst=[func,func,func]
print(lst)
这个列表的每一个元素都是函数func里面的内存地址
Function name can be used as function parameters
def func():
print(1)
def func1(fn):
print(2)
fn() #执行传递过来的fn
print("666")
func1(func) #吧函数func当成参数传递给func1的参数fn
It can be used as the return value of the function
def func():
print(1)
def fun():
print("看我")
return fun
fun=func()
fun()
1, 看我
f format
f-strings are added to the start of new writing python3.6 formatted output,% s or higher before the formatted output format and more simplified than
Results F / f + str form, the position of the character desired to be replaced with {} placeholder in the exchange, and similar format, but this can be directly identified
name=1
age=2
s=f'姓名:{name},性别:{age}'
print(s)
可以加任意表达式
print(f'{2*3}') #6 直接运算结果
name='song'
print(f'全部大写:{name.upper()}') SONG
字典也可以
teacher={'song':'jiafan'}
print(d"the teacher is {teacher['name']}) #大括号里查询字典键就可以导员元素
列表
lst=['song','jiafan']
s=f'姓名:{lst[0]},年龄:{lst[1]}
print(s)
也可以插入表达式
def func(a,b):
return a+b
a=1
b=2
print(f'和+{func(a+b)}')
多行F
name='1'
age=2
sex=1
print(f'姓名{name}\
性别{sex}\
年龄{age}')
Other details
print (f '{{{g}}}') to print out the two braces is that it is a four braces inside the braces can not appear two punctuation: {};!
Iterator
Iterables defined: There are many private methods, support __iter __ () method is iterable
Iterator: Support __iter __ () method is the iterator and __next __ () method
- See inside the object is to look at the method of the source is through dir () to determine which of the methods which in the end
s=123
print(dir(s))
dir会返回一个列表,这个列表含有该对象的整型的所有方法名我们就可以判断是不是可迭代对象了
print('__iter__()'in dir(s)) False 不支持
Iterative objects may be advantages: which can visually view the data,
Disadvantages: total memory, iterables not value iteration, the index out, other than key
The for loop is made in the bottom of a small conversion values can iteration, the iteration will be transformed into an iterator objects
Iterator
Defined iterator: ** in python, and internal containing __Iter__ method objects with the method __next__ iterators. **
Can be judged by judging next iter method and method which is an iterator, which is iterables file handle is an iterator rest of the list tuple dict str set are iterables
Iterative how objects can be converted into an iterator
lst=[1,2,3,4,5] s=lst.__iter__() s=iter(lst) print(s)
Iterator values: iterable is not been iteration values of (out of the index, slicing and key) but can be converted into iterator that he is using the __next __ () value of
lst=[1,2,3,4] s=lst.__iter__() print(s) #内存地址 s1=s.__next__() print(s1) #迭代器取得是值 s1=s.__next__() print(s1) 迭代器是通过next取值的一个next取对应的一个值,如果迭代器里面的值取完了还有next那就StopIteration报错
while the internal mechanism for loop simulation
cyclic object for the object must be iterative, it is not intended object can be iterative value, because the internal mechanism for loop is: to convert into iterables iterator, with the value then next, and finally the use of exception processing StopIteration exceptions thrown
s=[1,2,3,4,5] s1=s.__iter__() #将可迭代对象转化成迭代器 while True: 利用while循环,next进行取值 try: 利用异常处理终止循环 print(s1.__next__()) except StopIteration: break
Iterator advantages: Provincial memory, iterators in memory blame only the equivalent of a data space, the data will be in memory because a release, load the current data on each of the entry value
Inert mechanism: next time, take a value not too much value
Iterator disadvantages:
Quality data can not view the inside of the tube
The value is not turning back, only has value down
Iterative comparable objects and iterator
Iterables flexible operation, more private method, such as what CRUD, more intuitive, but take up memory, but can not direct loop iteration value of such a data set
Application: If you focus on flexibility for data processing, and sufficient memory space, the data set is set to be an iteration object that is the clear choice
Iterator: the memory is a very economical, can record the position values, the values may be directly added next for loop method, but not intuitive method of operating a single set of data comparing
Application: When your memory when space is tight, when the data is too large, the data set is set iterator is a good choice,
Recursive: two conditions are met
- Itself calls itself
- There are definite termination condition
Generator, derivation, a built-in function
1. Generator
there are three ways to create a python generator
- By function generator
- By derivation generator
- python module or built-in functions, the python generator function provided
- The yield is the return into the generator
def func():
print(111)
yield "222"
g=func() #这个时候函数不会执行,而是获取到生成器
print(g.__next__()) #这个时候函数才会执行
#并且yield会将func生产出来的数据222给了g.__next__()
结果
111
222
return and yield are returned
yield will record the execution position
return could write more, but only once, the function can be terminated, and to the caller function's return value knowledge
yield can write multiple records the execution location, you can also return many times, does not terminate function, next will yield the corresponding content
def func(): yield "hehe" yield "hehe" yield "hehe" g=func() ret=g.__next__() print(ret) ret1=g.__next__() print(ret1) ret2=g.__next__() print(ret2) #必须一一对应 要不会报错 一个yield对应一个next
5.yield can be temporarily suspended for while loop inside the function
6.next () == __ next __ () iter () == __ iter __ () python2 and python3 in both next () python2 only next () method recommended next ()
send () method (to understand)
The difference between the send and next
The same point: send and next () so that the generator can yield a corresponding downward once, can obtain the yield value generated
Different: yield acquired first value is not used only send (can send (None)) with the next send a yield value can be passed on to the value
yield from
python3 provide a direct each iteration to be a data object is returned as the result generator
The yield iterable elements one by one return
def func():
lst=[1,2,3,4,5]
yield lst
g=func()
print(g) #返回func内存地址
print(next(g)) #只是返回一个列表
def func():
lst=[1,2,5,3,4]
yield from lst
g=func()
print(g) #内存地址
print(next(g))# 返回的是一个列表的元素 多了也会报错
def func():
lst=[1,2,3,4,5]
lst1=[9,8,7,6,5]
yield from lst1 #先执行这个lst1列表的元素
yield from lst #挨个返回后再执行lst的元素
g=func()
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
2. derivations
- List comprehensions
向列表中添加1~10
print([i for i in range(10)])
简洁 推导这个从0-9十个数的打印
lst=[]
for i in range(10):
lst.append(i)
print(lst)
List comprehensions divided into two modes:
- Circulation mode: [variables (process variables) for the variable in iterable]
- Filter Mode: [variables (process variables) for the variable in iterable if conditions]
3. cycle mode
lst=[i*i for i in range(10) ] 十以内数字的所有整数平方 这个i*i就是加工的变量 lst=[f'python{i}期'for i in range(10)] print(lst) 从python0期到python9期 f'python{i}期'就是加工的变量
3. Filter mode
Filtering mode is to add a condition on the basis of the above determination, then the condition is added to the list
把lst=[1,2,3,4,5,6,7,8]这个列表中的大于三的留下来 lst=[1,2,3,4,5,6,7,8] print([i for i in lst if i>3]) 把列表元素长度小于三的筛选掉 把剩下的全部大写 l = ['wusir', 'laonanhai', 'aa', 'b', 'taibai'] print([i.upper() for i in l if len(i)>3])
找到嵌套列表中名字含有两个‘e’的所有名字(有难度) names = [['Tom', 'Billy', 'Jefferson', 'Andrew', 'Wesley', 'Steven', 'Joe'], ['Alice', 'Jill', 'Ana', 'Wendy', 'Jennifer', 'Sherry', 'Eva']] print([name for lst in names for name in lst if name.count('e') >= 2]) # 注意遍历顺序,这是实现的关键
4. generator expression
Generating a list of expressions and a grammar derived exactly the same formula, but the [] with () on the line
比如八十以内的所有丨的平方放到一个生成器表达式中 s=(i**2 for i in range(10)) print(s) 得到是内存地址 #生成器表达式也可以进行筛选 获取1-100能被三整除的数 s=(i for i in range(100) if i%3==0) for i in s: print(i)
And generating a list of expressions derived distinction formula:
- List comprehension memory consuming all-time data is loaded into memory. Follows expressions generated iterator protocol, generating an element-by-
- The resulting values are different, a list of derived type is obtained a list generator expression is obtained by a generator.
- List comprehensions glance generator expression of knowledge a memory address.
Inert mechanism Generator: The generator only if the value of the time of the visit, asked him to just give you value, do not will not be executed
Dictionary derivations:
lst=[1,2,3] lst1=["nihao","hello","hey"] dic={lst[i]:lst[i] for i in range(len(lst))} print(dic)
A set of derivation; generating a set of unordered set of features is not repeated so natural to marry derivation weight
print({i for i in [-13,-13,2,2,3] })
3. a built-in function
s = """for i in range(10): print(i)"""s1 = """def func(): print(123)func()"""print(eval(s))print(exec(s1)) # 牛逼 不能用print(hash("asdfas"))print(help(list))help(dict)def func(): passprint(callable(func)) # 查看是否可调用print(float(2)) # 浮点数print(complex(56)) # 复数print(oct(15)) # 八进制print(hex(15)) # 十六进制print(divmod(5,2)) # (2, 1) 2商 1余print(round(5.3234,2)) # 四舍五入 -- 默认是整数,可以指定保留小数位print(pow(2,3)) # 幂print(pow(2,3,4)) # 幂,余s = "alex"print(bytes(s,encoding="utf-8"))print(ord("你")) # 当前编码print(chr(20320))s = "C:\u3000"print(repr(s))print("\u3000你好")lst = [1,2,3,False,4,5,6,7]print(all(lst)) # 判断元素是否都为真 相似andprint(any(lst)) # 判断元素是否有真 相似orname = 1def func(): a = 123 # print(locals()) # print(globals())func()print(globals()) # 全局空间中的变量print(locals()) # 查看当前空间的变量
These are not commonly used built-in functions, you can understand