08day

I. Review of the previous lesson content
    1. Basic knowledge supplement
        join () to convert into a list of strings
        split () cutting. The string becomes list
 
        Delete the problem.
            list and dict
            Delete list can not be deleted when the cycle of
            dict do not change the size of the time cycle.
 
            Need to delete the contents stored in a list. This list is circulating delete the old list
        fromkeys (a, b) to each element in a acquired and assembled into a new dictionary b Back
 
If the file operation when the picture is no need to write encoding
r + whether you read how much content, where the cursor is, when at the end of the writing is written; preferably with simultaneous read and write mode
w + first read after write, less direct write will clear the file contents
a+
b non-text format
Cursor movement
seek (offset, position)
seek 3bytes a Chinese; a Chinese accounted for 3 bytes
seek (0) # beginning seek (0,2) offset in the end 02 is representative of the end of the (0,1) position of the current
The file operation process
 
1 Open the file, get the file handle and assigned to a variable
2 by operating the file handle
3 close the file
 
r, read-only mode (default).
w, write-only mode. [Unreadable; does not exist, create; there is deleting content;],
If the file exists, then empty the file first and then go in; open for writing, truncating the file first
a, additional write mode. [Does not exist it is created; there is only the additional content;] open for writing, appending to the end of the file if it exists
 
"+" Indicates a file can read and write at the same time
r +, can read and write files. [Read; write;] can be added, to read and then append or will be covered directly (most used); If the file does not exist will not create a file, read and write, does not create the file does not exist, writing starts from the top, it will overlay content prior to this position, such as the write data from the write pointer to the current time;
 
If we open the file, without any read-write, from the addition of the end
w +, write read: useless) is first written content will be added later in the file (the original file will be overwritten)
 
rb read binary files
 
"B" represents the binary files (eg: for an FTP transmission marked when ISO file uploading, linux negligible, windows handle binary files)
rb
wb
from
for line in f: # f is a time iterator thing; efficient cyclic process
  print (line) # read line by line, and memory only holds one row; the highest efficiency
 
 
To avoid forgetting to close after opening the file, you can manage context
After Python 2.7, with another simultaneous support for multiple file's context management
with open('log1') as obj1, open('log2') as obj2:
pass-
 
When the with code block is finished, the interior will automatically shut down and release the resource file
 
for line in f: # f is a time iterator thing; efficient cyclic process
  print (line) # read line by line, and memory only holds one row; the highest efficiency
 
with open ( "format_test.py", "r +") as fp: # To read the entire file in this way, use the least memory
    for i in fp:
    print(i)
 
readline () # reads a line
__next __ () # read the next line: fp .__ next __ ()
readlines () # reads all the contents, and returns a list (a list of the behavior of an element value): Read and return a list of lines from the stream: this operation can not, take up too much memory; read a content
write() #写一行数据以unicode Write the unicode string s to the stream and return the number of characters written.
writelines (list) # write multiple lines, parameter list, the list needs to wrap strings, or will be written into one line: Write a list of lines to the stream
seek () # handle pointer operation, reaches a certain position, for moving the read pointer to the file location specified
tell () # handle to get the current pointer position; according to the number of characters
truncate () # taken header file handle to the current position of the character string, returns None; scratch truncation, if the input, taken from the specified start position, i.e., deleted
flush () # hard to force a refresh
seekable () # Return True if the stream supports random access; terminal type non-return file
writeable() #Return True if the stream supports writing
name #print (fp.name) Returns the file
 
dict.fromkeys('abc',666)                   
 {'a': 666, 'b': 666, 'c': 666}    
 
 
str The split method is based on the default blank split

Guess you like

Origin www.cnblogs.com/pythonwork/p/11493775.html