Python Day13 document processing function

Files modified in two ways

 
# Files that were modified in two ways 
# One way: text editor is used in this way
# realization of ideas: the contents of the file is read into memory all at once issued, then the modification is completed and then written back to cover the original file in memory
# advantages: in the course of only one copy of the file to modify the same data
# disadvantages: it will take up too much memory
# with Open ( 'c.txt', the MODE = 'RT', encoding = 'UTF-8') AS f:
# RES = reached, f.read ()
# = res.replace Data ( 'Alex', 'DSB')
# Print (Data)
#
# with Open ( 'C.txt', MODE = 'wt', encoding = 'UTF-. 8') f1 AS:
# f1.write (the Data)


# Second way:
Import os
# realization of ideas: to open the original file read, write way to open a temporary file, line by line to read the contents of the original document, written after modification ... temporary files, delete the original file, the temporary file is renamed original file name
# advantages: do not take up too much memory
# Cons: file modification process saved two same data
with open ( 'c.txt ', MODE =' RT ', encoding =' UTF-. 8 ') AS F, \
Open ('. c.txt.swap ', MODE =' wt ',encoding='utf-8') as f1:
for line in f:
f1.write(line.replace('alex', 'dsb'))

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

Use basic functions

 
Function 
1, is a function of what
function is equivalent tool has a function of
using the function must follow one principle:
to define
what happened defined function
1, application memory space to save the code function body
2, the above-mentioned memory address binding function name
3, define the function does not execute the function body code, but will detect body of the function syntax
called after
what happened the called function
1, function by function name to find the address of the memory
2, and then in parentheses is the trigger code execution function body
2, why of function
1, the organizational structure is not clear, poor readability
2, code redundancy
3, maintainability, scalability, poor


grammar defined functions
# def function name (parameter 1, parameter 2, ...):
# "" " document describes "" "
# function body
# return value



3, the function how
to define
three definitions embodiment
forms a: no function parameter
Two forms: with a reference function (equivalent material parameters, return value is equal to the product return)
in the form of three: empty functions, function body pass code
using the scene look at the situation

after the call to
three kinds called by
calling function
1, the statement of the form: just add brackets calling function
2, an expression form:
# assignment expression
# RES = the Add (1,2)
# Print (RES)
# math
# RES = the Add (1,2) * 10
# Print (RES)
. 3, function call parameters can be used as


the return value of
three kinds of return value in the form of
function return values
# return is a function of the end of the sign, that is a function of the body once the code is running to return immediately
# Terminates the function, and will return a value obtained as a result of this operation returns:
1, returns None: no return in vivo function
2, a value returns: return value
3, the plurality of return value: separated by commas a plurality of values, will be returned to the return tuple
# Files that were modified in two ways 
# One way: text editor is used in this way
# realization of ideas: the contents of the file is read into memory all at once issued, then the modification is completed and then written back to cover the original file in memory
# advantages: in the course of only one copy of the file to modify the same data
# disadvantages: it will take up too much memory
# with Open ( 'c.txt', the MODE = 'RT', encoding = 'UTF-8') AS f:
# RES = reached, f.read ()
# = res.replace Data ( 'Alex', 'DSB')
# Print (Data)
#
# with Open ( 'C.txt', MODE = 'wt', encoding = 'UTF-. 8') f1 AS:
# f1.write (the Data)


# Second way:
Import os
# realization of ideas: to open the original file read, write way to open a temporary file, line by line to read the contents of the original document, written after modification ... temporary files, delete the original file, the temporary file is renamed original file name
# advantages: do not take up too much memory
# Cons: file modification process saved two same data
with open ( 'c.txt ', MODE =' RT ', encoding =' UTF-. 8 ') AS F, \
Open ('. c.txt.swap ', MODE =' wt ',encoding='utf-8') as f1:
for line in f:
f1.write(line.replace('alex', 'dsb'))

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

Use basic functions

 
Function 
1, is a function of what
function is equivalent tool has a function of
using the function must follow one principle:
to define
what happened defined function
1, application memory space to save the code function body
2, the above-mentioned memory address binding function name
3, define the function does not execute the function body code, but will detect body of the function syntax
called after
what happened the called function
1, function by function name to find the address of the memory
2, and then in parentheses is the trigger code execution function body
2, why of function
1, the organizational structure is not clear, poor readability
2, code redundancy
3, maintainability, scalability, poor


grammar defined functions
# def function name (parameter 1, parameter 2, ...):
# "" " document describes "" "
# function body
# return value



3, the function how
to define
three definitions embodiment
forms a: no function parameter
Two forms: with a reference function (equivalent material parameters, return value is equal to the product return)
in the form of three: empty functions, function body pass code
using the scene look at the situation

after the call to
three kinds called by
calling function
1, the statement of the form: just add brackets calling function
2, an expression form:
# assignment expression
# RES = the Add (1,2)
# Print (RES)
# math
# RES = the Add (1,2) * 10
# Print (RES)
. 3, function call parameters can be used as


the return value of
three kinds of return value in the form of
function return values
# return is a function of the end of the sign, that is a function of the body once the code is running to return immediately
# Terminates the function, and will return a value obtained as a result of this operation returns:
1, returns None: no return in vivo function
2, a value returns: return value
3, the plurality of return value: separated by commas a plurality of values, will be returned to the return tuple
Function 
1, is a function of what
function is equivalent tool has a function of
using the function must follow one principle:
to define
what happened defined function
1, application memory space to save the code function body
2, the above-mentioned memory address binding function name
3, define the function does not execute the function body code, but will detect body of the function syntax
called after
what happened the called function
1, function by function name to find the address of the memory
2, and then in parentheses is the trigger code execution function body
2, why of function
1, the organizational structure is not clear, poor readability
2, code redundancy
3, maintainability, scalability, poor


grammar defined functions
# def function name (parameter 1, parameter 2, ...):
# "" " document describes "" "
# function body
# return value



3, the function how
to define
three definitions embodiment
forms a: no function parameter
Two forms: with a reference function (equivalent material parameters, return value is equal to the product return)
in the form of three: empty functions, function body pass code
using the scene look at the situation

after the call to
three kinds called by
calling function
1, the statement of the form: just add brackets calling function
2, an expression form:
# assignment expression
# RES = the Add (1,2)
# Print (RES)
# math
# RES = the Add (1,2) * 10
# Print (RES)
. 3, function call parameters can be used as


the return value of
three kinds of return value in the form of
function return values
# return is a function of the end of the sign, that is a function of the body once the code is running to return immediately
# Terminates the function, and will return a value obtained as a result of this operation returns:
1, returns None: no return in vivo function
2, a value returns: return value
3, the plurality of return value: separated by commas a plurality of values, will be returned to the return tuple

Guess you like

Origin www.cnblogs.com/AaronY/p/12514858.html