# Python collection, file handling, character encoding

Source: Internet
Author: User
Tags file handling

1. Set operation : Remove weight

{1, 2, 3, 4, 5, 6, 7, 8, 9}

`1 list1 = [1,2,3,4,5,5,6,7,8,9]2 list1 = Set (list1)3print( LIST1)`
View Code

1.1

There is a intersection in the collection syntax to ask for the intersection of two sets

`1List1 = [1,2,3,4,5,5,6,7,8,9]2List1 =Set (List1)3 Print(List1)4 5List2 = Set ([0,1,2,3,44,44,55,555,666,7777])6 Print(LIST1,LIST2)7 8List1.intersection (LIST2)#intersection9 Print(List1.intersection (list2))Ten  OneThe result is: {1, 2, 3}`
View Code

1.2

In the set syntax there is also the union set operation

`1 list1.union (List2) 2 Print (List1.union (list2)) 3 4 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 7777, 555,, 666}5 The results are all printed in two sets.`
View Code

1.3

The difference set in the collection

`Print (List1.difference (LIST2)) #  {4, 5, 6, 7, 8, 9}`

1.4

Subsets in the collection

`1 # subset 2 Print (List1.issubset (LIST2)) # determine if List1 is a subset of List2 3 False`
View Code

1.5

The parent set of the collection

`1 Print (List1.issuperset (LIST2)) # determine if List2 is List1 's parent set 2 False`
View Code

1.6

Symmetric difference set of a set

`1 Print (List1.symmetric_difference (LIST2)) # Inverse Difference Set 2 3 {0, 7777, 4, 5, 6, 7, 8, 9, 555,, 666} # The intersection of the two sets is reversed`
View Code

1.7 If there is no intersection between two sets

`1 Print (List1.isdisjoint (LIST2)) # determine that two sets do not have an intersection return True`
View Code

1.8

Collection Delete

`1 Print (List1.pop ()) # Delete any Content`
View Code

1.9

`1 list1.discard# no error when deleting an element 2 {2, 3, 4, 5, 6, 7, 8, 9}`
View Code

2. File operation

`1 were the shadow to my light2 you are a shadow in the light of my life3 Did you feel US4 can you understand us?5 another Star6 another planet .7 You fade away8 you Fade away .9Afraid our aim isOut of sightTen fear our goal is lost in the field of Vision One wanna see us A I hope we understand each other . - Alive - stay alive. the Where is your now - Where are you? - Where is your now - Where are you? + Where is your now - Where are you? +Was it allinchMy Fantasy A is it all in my imagination at Where is your now - Where are you? - were imaginary - you're just unreal, not there ? - Where is your now - Where are you? in Atlantis - Atlantis to under the Sea + in the seabed - under the Sea the in the seabed * Where is your now \$ Where are you?Panax Notoginseng another dream - another dream. theThe Monster's Running wild inside of me + wild Monsters are in my heart AI'm faded the I'm so haggard and miserable +I'm faded - I'm so haggard and miserable \$So lost, I'm faded \$ so lost and haggard -I'm Faded ~ ~ ~ - I'm so haggard and miserable theSo lost, I'm faded - so lost and haggardWuyi these shallow waters never met the The shadows in the water that I've never seen before - What I needed Wu I need it. -I'm Letting Go About It's just natural . \$ A Deeper Dive - Deep seabed - Eternal Silence of the sea - The endless Silence in the sea AI'm Breathing + the sound of my breath the Alive. - stay alive. \$ Where is your now the Where are you? the Where is your now the Where are you? the under the bright but faded lights - The bright lights are fading in You set my heart on fire the you lit up my firelight. the Where is your now About Where are you? the Where is your now the Where are you? the ... + ... - Where is your now the Where are you?Bayi Atlantis the Atlantis the under the Sea - in the seabed - under the Sea the in the seabed the Where is your now the Where are you? the another dream - another dream. theThe Monster's Running wild inside of me the wild Monsters are in my heart theI'm faded94 I'm so haggard and miserable theI'm faded the I'm so haggard and miserable theSo lost, I'm faded98 so lost and haggard AboutI'm Faded ~ ~ ~ - I'm so haggard and miserable101So lost, I'm faded`
View Code
`2.1.1 F = open (' C ')Print (F.readlines ()) #默认读出所有行, and a line appears, after each sentence \ n2.1.2 F = open (' C ')Print (F.read ()) #默认读出全部行`
`2.1.3`
`Print (F.readline ()) #默认读出第一行2.2  Open File mode has the following types of`
• R, read-only mode (default).
• W, write-only mode. "unreadable; not exist; create; delete content;"
• A, append mode. "Readable; not exist" create; "only append content;"

"+" means you can read and write a file at the same time

• r+, can read and write files. "readable; writable; can be added"
• w+, write and read
• A +, with a

2.3 See below for some common uses of the file

2.3.1

`f = open (' C ')`
`Print (f.encoding) #打印出编码格式UTF-8`
`To use the Flush feature, see:`
` 1  import   sys  2  import   Time  3  4  for  i in  range (10  5  sys.stdout.write ("  #   "   6   Sys.stdout.flush ()  7  time.sleep (1 8  #   `
View Code
`F.truncate (#默认不写数字的话), the whole will be emptied.2.3.2`
`f = open (' C ', ' w+ ', encoding= "Utf-8")#r + can read and write name: Read and write  #w + Write a file first, then write it in#a + Append ReadF.write ("Wo is llll\n")F.write ("Wo is lei\n")F.write ("Wo is leileilei\n")F.write ("Wo is leileilei\n")Print (F.tell ()) #打印当前光标所在位置Print (F.seek) #光标跳会到某一位置F.write ("Could I working python?") Tell me? Why not? ")F.close ()2.3.3 File Modification`
`1 #The following code implements a character in a change file2f = open ('C','R', encoding="Utf-8")#First we read from a file, which is equivalent to the source file3f_new = open ('D','W', encoding="Utf-8")#creates a new file, writes the replaced file to the file4 #Old_str5  forLineinchF:#looping through source files6     if "more than the joy of the wanton" inchLine#judging the character "more wanton pleasures" in no longer per line7line = Line.replace ("more than the joy of the wanton","how painful the torture")#replace the characters in the source file with the8F_new.write (line)#writing to a newly created file will result in a new file, and a character that has been replaced`
View Code

3. Detailed character encoding

Need to know:

1. In Python2 the default encoding is ASCII, python3 default is Utf-8

2.unicode is divided into utf-32 (4 bytes), utf-16 (two bytes), Utf-8 (1-4 bytes), so Utf-8 is Unicode

3. Encode in Py3, while transcoding will also change the string to bytes type, decode decoding will also turn bytes back to string

Understanding the graph and then encountering the character encoding will not panic.

Python collection, file handling, character encoding

Related Keywords:
Related Article

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.