Data types and Files 1.1 python dictionary
Dictionary writing Format:
- #!/usr/bin/env python
- #key-value
- info = {
- ' Stu1101 ': "zhangsan",
- ' Stu1102 ': "lizi",
- ' Stu1103 ': "wangwu",
- }
- Print (info)
- Print ("select-->", Info["stu1101"])
- #names = info["stu1101"]
- #print (names)
- Print ("get-->", info.get ("stu1102")) #get方法
- Execution Result:
- {' stu1103 ': ' Wangwu ', ' stu1101 ': ' Zhangsan ', ' stu1102 ': ' Lizi '}
- Select--> Zhangsan
- Get--> Lizi
- info["stu1101"] = "add 1101"
- Print (info)
- info["stu1104"] = "xiaosan" #不存在则增加
- Print (info)
- Execution Result:
- {' stu1103 ': ' Wangwu ', ' stu1101 ': ' Add 1101 ', ' stu1102 ': ' Lizi '}
- {' stu1103 ': ' Wangwu ', ' stu1104 ': ' Xiaosan ', ' stu1101 ': ' Add 1101 ', ' stu1102 ': ' Lizi '}
- Del info["stu1103"]
- Print (info)
- Execution Result:
- {' stu1102 ': ' Lizi ', ' stu1101 ': ' Zhangsan '}
- Multilevel Dictionary Nesting
- #!/usr/bin/env python
- www = {
- "personal Blog": {
- ' Name ': ["lizhong"],
- ' Address ': ["cloudstack.com"],
- },
- "ops Community": {
- ' Name ': ["bjstack"],
- ' Address ': ["bjstack.com"],
- },
- }
- Www["personal blog" ["address"][0] + = ", very good personal blog, a lot of attention!"
- Www["operation and Maintenance community" ["address"][0] + = ", Community activity is very high, look forward to your joining!"
- Print ("bash surprise--", www["personal blog" ["address"])
- Print ("bash surprise--", www["ops community"] [address])
- Execution Result:
- Bash has surprise--[' cloudstack.com, very good personal blog, lots of attention! ']
- Hit with surprise--[' bjstack.com, community activity is very high, look forward to your joining! ']
1.2 Set Set
1. Go to the weight, turn a list into a set, and then automatically go heavy.
2. Relationship test, test the intersection of two sets of data, difference set, and set the Relationship.
Create a collection of values
- List_1 = set{1,4,5,7,3,6,7,9}
- List_2 = set{2,6,0,66,22,8,4}
- Intersection (list_1 & List_2)
- Print (list_1.intersection (list_2))
- Print (list_1.union (list_2))
- Print (list_1.difference (list_2))
- Print (list_2.difference (list_1))
- Print (list_1.issubset (list_2))
- List_3 = Set ([1,3,7])
- Print (list_3.issubset (list_1)) #list_3是list_1的子集
- Print (list_1.issuperset (list_3))
- Symmetric difference set (list_1 ^ list_2)
- Print (list_1.symmetric_difference (list_2)) #对称差集, removed from each other, removed two two sets of duplicate
Inverse difference Set
Add to
- List_1.add (999) #添加一项
- List_1.update ([888,777,555]) #添加多项
- Print (list_1)
- Print (list_1.discard (555)) #discard不存在不会报错
- Print (list_1.remove (888)) #remove不存在, will be error, deleted and will not return data
- Print (list_1)
1.3 File operations
- File handle = filename (' file path ', ' mode ')
- Note: open files in Python in two ways, open (...) and file (...) in essence, the former will call the latter for the operation of files, it is recommended to use Open.
- File mode has been canceled in Python3
- Open File Modes are:
- f = Open (' db ', ' r ') # read-only mode, {default}
- f = Open (' db ', ' W ') # write-only mode, {unreadable; not exist then create; delete content;}
- f = Open (' db ', ' a ') # append mode, {readable; not exist then create; only append content;}
- f = Open (' db ', ' x ') # file exists, error is present, no content is created and written
- "+" means that you can read and write to a file:
- f = Open (' db ', ' r+ ') # can read and write files, {readable; writable; can append}
- f = Open (' db ', ' w+ ') # write Read.
- f = Open (' db ', ' A + ') # with A.
- "U" means that \ r \ n can be automatically converted to \ n (in conjunction with R or r+ Mode) when reading
- f = Open (' db ', ' RU ')
- f = Open (' db ', ' R+u ')
- "b" means processing binary files (such as FTP upload ISO image, linux ignore, Windows processing binaries need to be labeled)
- f = Open (' db ', ' RB ') # binary Read-only
- f = Open (' db ', ' WB ') # Binary write only
- f = Open (' db ', ' ab ') # binary Append
- f = Open (' db ', ' r ')
- data = F.read ()
- Print (data,type (data))
- F.close ()
- f = Open (' db ', ' r+ ', encoding= "utf-8")
- data = F.read (1) # If open mode is no b, then read, reads by character
- Print (f.tell ()) # Tell where the current pointer is (in bytes)
- F.seek (f.tell ()) # Adjustment current pointing to your position (bytes)
- F.write ("777") # Current pointer position start overwrite
- Print (data) # printout
- F.close () # Close the current file
- Through the Source view function
- Read () # no arguments, reads all; parameter, b byte, no B by character
- Tell () # Gets the current pointer position (in bytes)
- Seek () # pointer jumps to the specified position (bytes)
- Write () # writes data, b: bytes, no B: character
- Close () # closing file
- Fileno () # File descriptor
- Flush () # flush File Internal buffer
- ReadLine () # Read Only one row
- Truncate () # intercept, empty after pointer position
- f = Open ("")
- For line in F:
- Print (line)
- # db file with "xuliangwei" string
- f = Open ("db", "r", encoding= "utf-8")
- f_new = open ("db.bak", "w", encoding= "utf-8")
- For line in F:
- If "xuliangwei" in Line:
- line = Line.replace ("xuliangwei", "xuliangwei.com")
- F_new.write (line)
- F.close ()
- F_new.close ()
- F.close () #直接close文件
- Avoid opening files after forgetting to close, unified through the management context, namely:
- With open (' db1 ', ' r ') as f1, open ("db2", ' W ') as F2:
- Pass
1.4 Python character encoding conversion
Asiica does not support Chinese
Utf-8 a man: three bytes
GBK a man: two bytes
The default character encoding in Python3 is Utf-8
- #!/usr/bin/env python
- name = ' Li Zhong '
- Print (name.encode (' UTF-8 ')) # to UTF-8 encoding
- Print (name.encode (' GBK ')) # to GBK encoding
- Print (name.encode (' ASCII ')) # to ASCII encoding
The default character encoding in python2.x is Unicode
Python (iii)