Section VII: iterators and generators

f.read (3) represent the data read three characters, if the file is open b mode, three bytes are read
f.flush files from the memory to the hard disk brush
f.closeed Close returns True if the file
f. View to open the file encoding using open coding


tell View cursor position
seek to move the cursor
truncate truncate file
0 mode, start moving the cursor position from the beginning, the default mode is 0, open to r mode on the line
1 mode, begin to move (to be read in a rb way files) from the last cursor position f.seek (10,1)
2 mode, starts from the end of the file to move the cursor backwards f.seek (-10,2), the end of the line there note windows \ r \ n, is used in the log file when writing

Way loop files:
for i in f: when used in this way will read the memory
for i in f.readlines (): This is the file is to read all the contents of the memory
for i in f:
f.seed ( -3,2)

iterator
recursive: they call themselves, the results will continue to be passed to deal with their own, until the final result
iterables: replacement, the results obtained are each based on a
mechanism for loop (very important)
mechanism while loop simulation for loop
li = [1,2,3,4,5]
li_iter = li .__ iter __ ()
while True:
the try:
Print (li_iter the Next .__ __ ())
the except StopIteration:
Print ( "iteration is completed, terminate print ")
BREAK

Builder: iterables is, without going through _iter_ () converts direct call method _next_ ()
generator function: the function family yiel return place, can yield from multiple, one can only Retrun
list Analysis: [ ' bb% s'% i for i in range (10)], ternary expressions with the rules, the [] with () becomes generator expression

 

Surrounding ternary expressions:
"SD" IF name == "SDD" the else "SB"
mono-, di- three yuan

Analytical list:
[ '% S BB' I% I in Range for (10)] to generate a list, the larger the value of memory will occupy when

Guess you like

Origin www.cnblogs.com/sxdpython/p/12657134.html