One, the data structure of Python-collection
1, definition of the collection
The collection in Python is enclosed in {}, for example, X=set ({1,2,3,4,5}), which is a collection of features: (1), de-heavy (2), unordered, the function of the set: as below, (1) to go to the weight, a list into a set, automatically go to the weight (2) Relationship test, To test the intersection, difference set, and set of two sets of data before the relationship
For the order of the collection here to say, not when you define the time cannot write duplicate elements, in fact, can write duplicate elements, and Python will not error, but in the output time, or in other aspects of processing, Python will have to go to the heavy collection to do the operation, for example:
X={1,2,2,3,4,4}print (x) {1, 2, 3, 4}
2, the method of the collection
(1) X.add (*) adds an element. X.pop () Deletes an element, X.remove (*) deletes a specified element
X={1,2,4,7}x.add (5) print (x) X.pop () print (x) x.remove (4) print (x)
(2) x.union (y) for the set of x and Y sets, x.difference (y) for elements in X but not in Y, X.intersection (y) to find the intersection of X and Y
1 x={1,2,4,7}2 y={4,5,6,2}3print(x.union (y))4 Print(x.difference (y))5Print(x.intersection (y))
(3) x.issubset (y) if x is contained in Y, returns True,x.issuperset (y) if x contains Y, returns True
1 x={2,4}2 y={4,5,6,2}3print(X.issubset (y))4 Print(X.issuperset (y))
(4) Len (x) Gets the length of the XJ collection and clears the data from the X collection
1 x={2,4}2print(len (x))3x.clear () 4 Print (x)
Two, Python file operation
1,open function syntax
Open (file[, mode[, buffering[, encoding[, errors[, newline[, Closefd=true]])
The Open function has a number of parameters, commonly used are file,mode and encoding
file location, quotation marks required
mode file opens, see the table below
encoding indicates what encoding is used for the returned data, generally using UTF8 or GBK;
Value of the 2,mode parameter
| Character |
meaning |
| ' R ' |
open for reading (default) |
| ' W ' |
open for writing, truncating the file first |
| ' a ' |
open for writing, appending to the end of the file if it exists |
| ' B ' |
binary mode |
| T ' |
text mode (default) |
| ' + ' |
open a disk F Ile for updating (reading and writing) |
| ' U ' |
Universal Newli NE mode (for backwards compatibility; Should is used in new code) |
where R, W, A is the basic mode of open files, corresponding to the read-only, write-only, append mode;
B, T, +, u these four characters, with the above file open mode combination, binary mode, text mode, read and write mode, universal line break, according to the actual situation combination of use,
(1) Open the file in R mode, where F is the file handle and belongs to a Memory object
1 f = open ("file.txt","R")2 Print(F.read ())
(2) Read the contents of the file
Read () reads the contents of the entire file, ReadLine () reads each row in a sequential order, readlines () reads all of the files, and each row as an element, stored in the list, the difference between the three, you can do a comparison of the following code
Read ()
1 f = open ("file.txt","R")2 Print(F.read ())3 f.close ()
ReadLine ()
1 f = open ("file.txt","R")2 Print(F.readline ())3print(F.readline ())4Print (F.readline ())5 f.close ()
ReadLines ()
1 f = open ("file.txt","R")2 Print(F.readlines ())
Note: (1) Read (), ReadLines () is a one-time file read out, loaded into memory, if the file is larger, this two kinds of reading is not desirable, will consume greater memory and time
(2) ReadLine and ReadLines () read the contents of the file will read the file's newline character "\ n", but you can use split ("\ n") to remove the newline character
(3) Be sure to file relationships when you are finished working with files, F.close ()
(4) In reading large files is, cannot use the read (), ReadLines () method, but can use a more efficient way to implement reading the entire file, in memory only one row
1 f = open ("file.txt","R")2 for in F:3 print(line)4 f.close ()
(3) Write file W "w" mode to re-create a new file and write content, so the file itself has a file, need to write the content, it is necessary to save the original contents of the file a copy
1 f = open ("file.txt","w")2 F.write ("1234")
(4) Append write a "a" is written to the file in Append mode, note that when the file is written in a way, you need to add "\ n" before writing the content, otherwise, the write will be appended to the last line, if the file does not exist, the new
1 f = open ("file.txt","a")2 F.write ("\n1234")
(5) r+ read-write mode, r+ readable and writable, does not create files that do not exist. If the file is written directly, it is written from the top, overwriting the contents of the previous location, and then appending the content at the end of the file if you read and write it first. Does not create a file that does not exist.
1 f = open ( " file.txt , " r+ " ) Print (F.read ()) 3 F.write ( " \n44555 " )
1 f = open ("file.txt","r+")2 F.write ("345678\n")3print(F.read ())
(6) w+ write mode, first create the file, then write, not very useful
(7) A + append plus read/write does not exist then create. It is important to note that when open () opens an additional read-write mode in a + mode, the pointer is at the end of the file because it is a. If you do read () at this point, Python discovers that the pointer position is EOF and reads to an empty string.
So after opening the file in a + mode, you need to use the method of seek (0) to point the file pointer to the file header before reading. That is to say, if there is no seek (0) F.read () will not read the contents of the file
1 f = open ("file.txt","a+")2 F.seek (0)3print(F.read ())4 f.write ("678 ")
(8) F.seek (),
File.seek () method standard format is: Seek (offset,whence=0)
Offset: The starting offset, which represents the number of bytes that need to move the offset
Whence: A definition of the offset parameter that indicates which position to start the offset from, and 0 represents starting at the beginning of the file, 1 representing starting at the current position, and 2 representing the end of the file.
(9) In the Open function, after opening the file, we need to close the file, but the following open file mode, do not need to execute close the file, the code is as follows
1 With open ("file.txt","w") as F:2 F.write ("1111")
Three, code conversion
Let's start by explaining the history of character encoding.
(1) ASCII (American Standards Code for information Interchange, US Information Interchange standard codes) is a set of computer coding systems based on the Latin alphabet, Mainly used to display modern English and other Western European languages. It is now the most versatile single-byte encoding system and is equivalent to ISO/IEC 646.
(2) The 127 characters of ASCII can represent all the available content in English. However, there is no support for Chinese, so there is an extension of ASCII,GBK, gb2312, gb2318 character encoding. This will be a good support for Chinese
(3) Only support Chinese, English is not enough, but also to support other languages, this appears the "Universal Code" Unicode. can support
Python Study the third day