Retake course day6 (collection of python basics 4 and file operations ),

Source: Internet
Author: User
Tags nfsd pycharm community edition

Retake course day6 (collection of python basics 4 and file operations ),

I. supplement the dictionary attribute Method

The key is of the constant type, and the dictionary can be quickly searched based on the hash index.

Immutable type int bool str tuple variable type: list dict set

1 clear: clear

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# d. clear () # print (d) # clear

2 Cory: Copy

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# a = d. copy () # print (a) # copy

3 fromkeys: generate a dictionary and a property method under the class

a=dict.fromkeys(['k1','k2','k3'],[1,2])print(a)

4 pop: Delete

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# d. pop ('k1 ') # print (d) # Delete by key

5 popitem: Delete the entire element

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# d. popitem () # print (d) # Delete the entire key-Value Pair

6 setdefault: add only without modification

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# d. setdefault ('k3 ', 'v3') # print (d) # Add an element

7 update: If it exists, it is new. If it does not exist, it will be added.

# D = {#'k1 ': 'v1', # 'k2': 'v2' #}# d. update ({'k2': 'v3 ', 'k3': 'v2'}) # print (d) # Add. If the key exists, modify the value.

2. Set and set functions

The set is a non-repeated value, and it is still unordered.

1 add: The add operation cannot be repeated.

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} #. add ('qq') # add. If there is one in the Set, # print (a) is not added)

2 clear: clear

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # a. clear () # clear # print ()

3 copy: copy

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = a. copy () # copy # print (B)

4 difference: only one set of difference sets exists

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'chengg'} # print (a-B) # difference set # a = {'fang ', 'jie', 'lei', 'dong ', 'Tao'} # B = {'fang ', 'rei', 'cheng'} # print (. difference (B) # view the difference set

5 difference_update: Find and update the difference set

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} #. difference_update (B) # print (a) # Find and update the difference set

6 into ric_difference: Find the symmetry difference set

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'chengg'} # print (a ^ B) # symmetric difference set # a = {'fang ', 'jie', 'lei', 'dong ', 'Tao'} # B = {'fang ', 'rei', 'cheng'} # print (. symmetric_difference (B) # view the symmetry difference set

7 symmetric_difference_update: returns and updates Symmetric Difference Sets.

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} #. effecric_difference_update (B) # print (a) # search for and update Symmetric Difference Sets

8. discard: delete an object.

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} #. discard ('jie') # print (a) # Delete

9 intersection: intersection

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'chengg'} # print (a & B) # intersection # a = {'fang ', 'jie', 'lei', 'dong ', 'Tao'} # B = {'fang ', 'rei', 'cheng'} # print (. intersection (B) # intersection

10 intersection_update: intersection and update

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} #. intersection_update (B) # print (a) # search for and update the Union

11 union: union

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'chengg'} # print (a | B) # union # a = {'fang ', 'jie', 'lei', 'dong ', 'Tao'} # B = {'fang ', 'rei', 'cheng'} # print (. union (B) # union

12 isdisjoint: determines whether there is no intersection

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} # print (. isdisjoint (B) # determine whether there is no intersection

13 iscubset: determine whether it is a subset

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang ', 'jie'} # print (B. issubset (a) # judge whether B is a subset of

14 issuperset: determine whether it is a parent set

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang ', 'jie'} # print (. issuperset (B) # judge whether a is the parent set of B

15 pop random Deletion

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # a. pop () # print (a) # Delete randomly

16 remove: Delete

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} #. remove ('fang ') # print (a) # delete an element

17 update: update

# A = {'fang ', 'jie', 'lei', 'dong', 'Tao'} # B = {'fang', 'rei ', 'hangzhou'} #. update () # print (a) # update

18 len: Length

# a={'fang','jie','lei','dong','tao'}# print(len(a))

In general, a set must be placed before the set property, and any data type can be placed after the property. The values in the set cannot be changed. {} The dictionary is used by default. To create a set, set () is used ()

3. File Operations

Common File Methods Right

1. r (read)

2, w (write): write only to the file; w +: But can be written, and can be read after writing.

3, a (append): append content, only append; a +: the file can be read after append.

# Writing files in bytes type # a = B '\ xe4 \ xbd \ xa0 \ xe4 \ xb8 \ xaa \ xe5 \ xa4 \ xa7 \ xe5 \ x82 \ xbb \ xe5 \ x8f \ x89 \ r \ n \ xe9 \ x82 \ xa3 \ xe4 \ xba \ xe5 \ xb0 \ xb1 \ xe6 \ x98 \ xaf \ xe4 \ xbd \ xa0' # f = open (r'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'wb') # B = f. write (a) # f. close () ### write data by w overwrite # f = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'w ', encoding = 'utf-8') # B = f. write ('dsdf ') # f. close () # f = open (r'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W + ', encoding = 'utf-8') # f. write ('dsfdfsd') # f. seek (0) # seek () specifies the cursor position # a = f. read () # f. close () # print (a) # a # append method # f = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'A', encoding = 'utf-8') # B = f. write ('dsdf ') # f. close () # Read after append # f = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'a + ', encoding = 'utf-8') # B = f. write ('dsdf ') # f. seek (0) # a = f. read () # f. close () # print (a) # Read data in bytes # f = open ('e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', mode = 'rb') # a = f. read () # f. close () # print (a) ### read in UTF-8 format # f = open ('e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', mode = 'R', encoding = 'utf-8') # a = f. read () # f. close () # print (a) ## first read in append # f = open ('e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', mode = 'r + ', encoding = 'utf-8') # a = f. read () # f. seek (3) # f. write ('jie') # f. close () # print ()#

File Attribute operations:

1 read: read files

# with open('E:\pycharm\pycharm\PyCharm Community Edition 2017.1.3\day7\jie.txt','r',encoding='utf-8')as f :#     obj=f.read()# print(obj)

2 write: write a file

# Format 2: # with open ('e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'w', encoding = 'utf-8 ') as f: # f. write ('yi ')

3 seek: Adjust the cursor position

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsfdfsd') # f. seek (0) # seek () specifies the cursor position # a = f. read () # f. close () # print ()

4 tell: the position where the cursor is read

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # a = f. tell () # Find the cursor position # print ()

5 writable: whether it can be written

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8 ') # a = f. writable () # determine whether data can be written # print ()

6 readable: whether it is readable

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # f. seek (0) # a = f. readable () # determine whether it is readable # f. close () # print ()

7 close: close the file

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs \ ndas') # f. close () # close

8 flush: flush to Hard Disk

# F = open ('e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', mode = 'r +', encoding = 'utf-8 ') # a = f. read () # f. seek (3) # f. flush () # flush to hard disk # f. write ('jie') # f. close () # print ()

9 fileno: file object

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs \ ndas') # a = f. fileno () # file object # f. close () # print ()

10 isatty: determines whether the device is output

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # f. seek (0) # a = f. isatty () # determine whether it is an output device # f. close () # print ()

11 readline: Read a row of content

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # f. seek (0) # a = f. readline () # Read Only the entire row # f. close () # print ()

12 seekable: determines whether a pointer can be set

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # a = f. seekable () # determine whether a pointer can be set # print ()

13 truncate: The content behind the cursor is no longer needed, as long as the content before the cursor is

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs \ ndas') # f. truncate (5) # specify the cursor position, and then retain the content before the cursor to delete the content after the cursor # f. close ()

14 readlines: read all the content and then monitor the memory.

# F = open (r 'e: \ pycharm \ PyCharm Community Edition 2017.1.3 \ day7 \ jie.txt ', 'W +', encoding = 'utf-8') # f. write ('dsdfdfsd \ nfsd \ nfgs ') # f. seek (0) # a = f. readlines () # Read all. The returned result is a list, element string # f. close () # print ()

In python2, xreadlines is equivalent to for line in f in python3:

# f=open(r'E:\pycharm\pycharm\PyCharm Community Edition 2017.1.3\day7\jie.txt','w+',encoding='utf-8')# f.write('dsdfdfsd\nfsd\nfgs')# f.seek(0)# for line in f:#     print(line)# f.close()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.