Python learns the third day of string encoding decoding, collections, files, functions

Source: Internet
Author: User
Tags shallow copy

1 Data Type--collection

    • Initialization

list = [1, 2,3,4,5]

Set1 = set (list)

    • The logical relationship of the collection

Intersection: Set3 = set1.intersection (Set2) or Set3 = Set1 & Set2

Set: Set1.union (Set2) or Set1 | Set2

Differential set: Set6 = Set1.difference (Set2) or Set6 = Set1-set2

Set5 = Set2.difference (set1) or set5 = Set2-set1

Symmetry difference set: Set5 + Set6 = set1.symmetric_difference (Set2) or Set5+set6 = Set1 ^ Set2

Determine if a is a subset of B: A.issubset (b) or whether B is the parent set of a, B.issuperset (a)

Determine if AB has intersection: A.isdisjoint (b) No intersection Returns True there is a intersection return FALSE

    • Methods of collections

Length: Len (set), determines whether the element is in set: X in Set, shallow copy: Set.copy ()

Add: Set.add ("AAAA")

Set.update ([1,2,3,4,5])

Delete: Set.remove ("AAA") No AAA error, Set.discard ("AAA") No AAA return None

Very magical, no change and check. Looks like it's not going to be directly in the collection.

2. File operation
    • Process of file operation: Open file get handle--action file line (traverse etc)--close file
    • Open file Get handle

method to compare Low: F = open ("File_path", "R", "Encoding=utf-8") to close the file after the operation is finished F.close

Tall on the way: run out of Auto close

With open ("File_path", "R", "Encoding=utf-8") as F1,

Open ("File_path", "R", "Encoding=utf-8") as F2:

Opening method Parameter Description:

R: Read-only, one-line read

W: Write only, write sequentially, this parameter will create a new file. Remember

A: Append, open a file and then only write from the back

R+: This reading is like R, writing is like a. If you don't move the cursor

w+: This is to create a blank file or overwrite an old file with the same name. Do not move the cursor can not read any data, but move the cursor is idle egg pain to do things

A +: Additional Reading. No matter how to move the cursor, only append, but do not move the cursor can not read the data, is also a design of the egg pain

RB: Read files in binary format, read bytes type binary, or video files, if you want to see 010100101100 can do this

WB: Binary Write, write 010110011010 go in, transmission bytes should be used to get

ru:\r\n turn on, Windows writes can get Linux inside run, however, who will use Windows Notepad to write script? Unless he felt he had no place to spend his time.

    • Manipulating files

Read the cursor move, write the cursor to move, and imagine the feeling of writing in a notepad.

Action cursor: F.tell () the character position (by number of characters) at which the current cursor is located. F.seek (0) cursor back to the beginning, F.seek (10) The cursor moves to the 10th character.

I feel like I'm not going to use it, complicated situations, God knows where your cursor will move to.

READ: F.readline () read a line, note the line where the cursor is located, the first row of the cursor moves one line

F.readlines () Read all the characters of the entire file into a list, this one. It's possible to spend the memory

F.read () reads all the characters of the entire file into a string, this: It's possible to spend the memory

F.read (5) Read 5 characters from the beginning of the cursor, but who will go with it?

Write: F.write ("Balabalabalabala") writes strings, writes sequentially

Traversal: 1. On a tall, fast way:

Count = 0

For lines in F:f is a file handle, an iterator

Print (line)

Count + = 1

Traverse 2. The way to make the memory explode: You can play it by yourself.

For Index,line in Enumerate (F.readlines ()):

Print (index, line)

Some other methods of egg-ache:

F.encoding the encoding of the file F.fileno () The file handle number, operating system maintenance F.isatty is not a terminal device file
F.name file name F.seekable can you move the pointer F.readable readable?
F.writable can write? F.closed is closed F.flush () cache full flush to hard drive, synchronization requires high availability
F.truncate () Why do you use it? We need to check. F.truncate (10) Truncate 10 a mode
    • Print progress bar

Import sys, TIME

For I in range (20):

Sys.stdout.write (">") standard output. Console

Sys.stdout.flush ()

Time.sleep (0.1)

Python learns the third day of string encoding decoding, collections, files, functions

Related Article

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.