Python Sixth Week Study notes (1)

Source: Internet
Author: User
Tags serialization vars

File operations Open action
    • Io.open (file, mode= ' R ', Buffering=-1, Encoding=none,errors=none, Newline=none, Closefd=true, Opener=none)
    • Returns a file object (Stream object) and a file descriptor. Failed to open file, return exception

    • Basic use:
      f = open("test")print(f.read())f.close()
    • Be sure to close it when you're done

    • There are two types of file access modes:
      Text mode and binary mode
The Open parameter file
    • The name of the file to open or to create. If you do not specify a path, the default is the current path
Mode modes
Parameters Description
R The default, which means read-only open, throws an exception if write is called. Throws a Filenotfounderror exception if the file does not exist
W Write-only open, if the file does not exist, the file is created directly, and if the file exists, the contents of the file are emptied
X Creates and writes a new file, throws a Fileexistserror exception if the file exists
A Write open, append if file exists
B Binary mode, independent of character encoding, byte operation using bytes type
T The default, text mode, to understand the bytes of a file according to a character encoding, operation by character
+ Read and write open a file. To the original read-only, write-only open to provide missing read or write ability, can not be used alone. The Get file object still follows the characteristics of R, W, A, X

R Read Only, Wxa write only
Wxa can generate new files

File pointers
    • Point to current byte position
      • Mode=r pointer starts at 0
      • Mode=a pointer starts at EOF
Tell ()
    • Show the current position of the pointer
Seek (offset[, whence])
    • Move the pointer position, offset moves the number of bytes,
    • Whence text mode may have different representations of operations in binary mode
    • Text mode

      • whence 0 Default value, indicating starting from scratch, offset can only be a positive integer
      • Whence 1 indicates that offset accepts only 0 from the current position
      • Whence 3 means starting with EOF, offset accepts only 0
    • In binary mode
      • whence 0 Default value, indicating starting from scratch, offset can only be a positive integer
      • Whence 1 indicates that offset can be positive from the current position
      • Whence 2 means that offset can be positively negative starting with EOF

        Binary mode supports the offset of any starting point, starting from the beginning, from the tail, and from the middle position.
        Backward seek can be hyper-bounded, but the forward seek, can not be super-bounded, otherwise throw an exception

Buffering: Buffer
    • -1 indicates the default buffer size
    • Binary mode: Use IO. Default_buffer_size Value Control
    • Text mode: If it is an end device, use row cache, otherwise the same as binary mode
    • 0 only used in binary mode, indicating off buffer
    • 1 only used in text mode, indicating row buffering (flush if you see a newline character)
    • Greater than 1 specifies the buffer size
Buffer buffers
    • One memory space, FIFO queue, buffer full or threshold reached, flush to disk
    • Flush ()

      • Write buffer data to disk
      • When close () is called, Flush () is called first
    • Encoding

      • Text-only mode use
      • Default value of None using default encoding (Windows Gbk,linux UTF8)
    • Errors

      • None and strict errors will throw an exception, ignore ignore
    • NewLine

      • Default None ' \ n ', ' \ R ', ' \ r \ n ' are treated as newline (both converted to/n)
      • "indicates that universal line breaks are not converted automatically
      • Other legal characters indicate that the specified character is a line break
    • Closefd
      • Close the file descriptor, True to close, False to leave the descriptor after the file is closed
Read
    • Read (Size=-1)
      • Size read number of characters or bytes negative or none read to EOF
Line Read
    • ReadLine (Size=-1)

      • Each time a row is read, size controls the number of characters or bytes that are read one line at a time
    • ReadLines (Hint=-1)
      • Read a list of all rows
Write
    • Write (s) returns the number of characters written
    • Writelines (lines) writes a list of strings
Other
    • Seekable ()
    • Readable ()
    • Writable ()
    • Closed
Context Management
    • Object FREED by Interpreter

    • Syntax: With ... as
      • A context-managed statement block does not open a new scope
      • Closing file objects automatically after statement block execution
Memory io Stringio
from io import StringIO
    • A text-mode buffer in memory that can be manipulated like a file object
    • When close is called, buffer is released

    • method is similar to file IO
    • GetValue ()

      • Get the full content
    • Advantages
      • Reduce data landing and improve operational efficiency
Bytesio
from io import BytesIO
    • Similar to Stringio
File-like Object
    • Class file object:
      • sockets, stdin, StdOut are class file objects
Path operation
    • We recommend using the Pathlib module, using the Path object to manipulate
      from pathlib import Path
Path stitching and decomposition operators/
    • Operator overloading
    • Path Object/Path object
    • Path object/String or string/path object
Parts
    • Decomposes a path, returning a tuple
      from pathlib import Pathp3 = Path.cwd()p3.parts

Output: ('/', ' data ', ' Mypythonobject ')

Joinpath
    • Concatenate multiple characters into the path object
Get path
    • STR Get path string
    • Bytes Get path byte
Parent Directory
    • Parent returns the Path class object
    • Parents returns the parent directory to iterate over objects, index 0 is the current direct parent directory
Other
  • The last part of the name directory
  • The extension of the last section of the suffix directory
  • Stem directory last section, no suffix
  • Suffixes returns a list of multiple extension names
  • With_suffix (suffix) supplement extension to path trailer, return new path, invalid extension presence
  • With_name (name) replaces the last part of the directory and returns a new path

  • CWD () returns the current working directory
  • Home () return to the current home directory

  • Is_dir () whether the directory
  • Is_file () is normal file
  • Is_symlink () whether soft link
  • Is_socket () whether the socket file
  • Is_block_device () whether the block device
  • Is_char_device () whether the character device
  • Is_absolute () is the absolute path

  • Resolve () returns the absolute path of the current path object and is parsed directly if it is a soft link
  • Absolute () returns an absolute path, it is recommended to use resolve

  • Exists () whether the directory or file exists
  • RmDir () Delete the empty directory. Does not provide a way to determine if the directory is empty
  • Touch (mode=0o666,exist_ok=true) Create a file
  • As_uri () returns the path as a URI, such as "FILE:///ETC/PASSWD"

  • mkdir (Mode=0o777,parents=false,exist_ok=false)

    • Parents whether the parent directory is created, True equals mkdir-p false, the parent directory does not exist, then the Filenotfounderror is thrown
    • EXIST_OK False when the path exists and throws Fileexistserror true, this exception is ignored
  • Iterdir () Iteration current directory

  • Glob () pass with the given pattern

  • Rglob () passes a given pattern, and the recursive directory returns a generator

  • Match () pattern matching, successful return true

  • Stat () View file properties

  • Lstat () with stat But if the link file is displayed, the file information itself
File operations
    • Open (mdoe= ' R ', Buffering=-1,encoding=none,errors=none,newline=none)

      • Similar to the built-in function open returns a file object
    • Read_bytes ()

      • Read with ' RB '
    • Read_text (Encoding=none,errors=none)

      • Read with "RT"
    • Path.write_bytes (data)

      • Write in WB
    • Write_text (Data,encoding=none,errors=none)
      • Write with ' WT '
OS Module
    • Os.name

      • Windows is Nt,linux is POSIX
    • Os.uname ()

      • Return System Information Linux only
    • Sys.platform

      • WinDOS display Win32 Linux display Linux
    • Os.listdir (' 0:/temp ')

      • Return to directory contents list
    • Os.stat (Path, *, Dir_fd=none, follow_symlinks=true)

      • Essentially call the Linux stat
      • Path: string or bytes of path, or FD file descriptor
      • Follow_symlinks true returns the information for the file itself, False if the soft-link connection shows the soft links themselves
    • Os.chmod (path, mode, *, Dir_fd=none, follow_symlinks=true)

      • Refer to the Linux chmod command
    • Os.chown (path, UID, GID, *, Dir_fd=none, follow_symlinks=true)
    • Refer to the Linux chown command
Shutil Module
    • Copyfileobj (FSRC, fdst[, length])

      • Copy content
    • CopyFile (SRC, DST, *, follow_symlinks=true)

      • Copy content
    • Copymode (SRC, DST, *, follow_symlinks=true)

      • Copy Permissions
    • Copystat (SRC, DST, *, follow_symlinks=true)

      • Copy metadata, including permissions
    • Copy (SRC, DST, *, follow_symlinks=true)

      • Copy content, permissions
    • Copy2 (SRC, DST, *, follow_symlinks=true)

      • Copy content, metadata
    • Copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2, Ignore_dangling_symlinks=false)

      • Recursive replication directory uses Copy2 by default
    • Rmtree (Path, Ignore_errors=false, Onerror=none)

      • Recursive removal with caution
    • Move (SRC, DST, copy_function=copy2)
      • Move files, directories recursively
CSV file
    • Comma-separated Values
    • Dividing rows and columns by row delimiter, column delimiter
    • CSV does not specify character encoding

    • The line delimiter is \ r \ n
    • Column separators are often comma or tab characters

If the field contains double quotes, commas, newline characters, you must enclose them in double quotation marks.
If the field itself contains double quotes, use two double quotes to indicate an escape

CSV module
    • Reader (CSVFile, dialect= ' Excel ', **fmtparams)

      • Returns a Dictreader object, which is a row iterator
    • Fmtparams can be set

      • Delimiter column delimiter, default comma
      • Lineterninator Line delimiter default/r/n
      • QuoteChar field reference symbol, default double quotation mark

      • Doublequote double Quote processing default true, Quochar display two, False use Eacapechar as QuoteChar prefix
      • Escapechar escape character, default none
      • Quoting specifies the double quotation mark rule. Quote_all all fields Quote_minimal special character fields quote_nonnumneric non-numeric fields quote_none do not apply quotation marks
    • Writer (csvfile, dialect= ' Excel ', **fmtparams)
      • Returning Dictwriter instances
      • The main methods of Dictwriter are Writerow, writerows
INI file
    • INI file format such as:
[DEFAULT]a = test[mysql]default-character-set=utf8a = 1000[mysqld]datadir =/dbserver/dataport = 33060character-set-server=utf8sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    • The contents of the brackets are called section
    • Each section is a key-value pair in key,value form, key is called option

Default section default must be uppercase

Configparser Module Configparserle Class
from configparser import ConfigParsercfg = ConfigParser()
  • A section can be treated as a key, and its corresponding value is a dictionary of key-value pairs that store option, that is, the INI file is a nested dictionary. Use an ordered dictionary by default

  • Read (filenames, Encoding=none)

    • Read files, filenames can be a separate file or file list
  • Sections ()

    • Returns the section list. Does not include the default section
  • Add_section (Section_name)

    • Add section
  • Has_section (Section_name)

    • Is there a section
  • Options (section)

    • Returns the options for a section, including the options for the default section
  • Has_option (section)

    • Is there a section option
  • Get (section, option, *, Raw=false, vars=none[, fallback])

    • The option value from the specified section, if not found, go to default to find
  • Getint (section, option, *, Raw=false, vars=none[, fallback])
  • GetFloat (section, option, *, Raw=false, vars=none[, fallback])
  • Getboolean (section, option, *, Raw=false, vars=none[, fallback])

    • Returns the specified type of data
  • Items (Raw=false, Vars=none)

    • Returns all section names and their objects
  • Items (section, Raw=false, Vars=none)

    • Returns the key value pair of option for the specified section two tuples
  • Set (section, option, value)

    • Sets the option=value of the specified section (Option,value must be a string), the section does not exist and throws an exception
  • Remove_section (section)

    • Remove section and its option
  • Remove_option (section, option)

    • Removes the specified option from the specified section
  • Write (FileObject, space_around_delimiters=true)
    • Writes all the contents of the current config to FileObject
Serialization and deserialization Defined
    • Serialization of serialization

      • Store an in-memory object and turn it into bytes---Binary
    • Deserialization deserialization

      • Restores a byte of a file to an in-memory object <-binary
    • Serialization saved to a file is persisted
    • Typically, the data is serialized and then persisted or transmitted over the network or deserialized from a file, a sequence of bytes received from the network
Pickle Library
    • Dump (obj, Protocol=none, *, Fix_imports=true)

      • Serializing an object to a bytes object
    • Dump (obj, file, Protocol=none, *, Fix_imports=true)

      • Serializes an object to a file object, which is deposited into a file
    • Loads (file, *, Fix_imports=true, encoding= "ASCII", errors= "strict")

      • Deserializing from a Bytes object
    • Load (bytes_object, *, Fix_imports=true, encoding= "ASCII", errors= "strict")
      • Read data deserialization from a file
Serialization and deserialization of experimental serialization applications
    • Generally used in the network transport, the data is serialized and transmitted over the network to the remote node, the service on the remote server will receive the data deserialized use
    • Note: The remote receive side must have a corresponding data type when deserializing, otherwise it will error. In particular, custom classes must have a consistent definition on the remote side
    • Most projects are not stand-alone, single-service. The need to transfer data to other nodes over the network requires a lot of serialization and deserialization
    • However, XML, Json, Protocol buffer, etc. are commonly used across platforms, across languages, and across protocols. Do not select pickle
JSON module
    • JavaScript Object Notation

    • JSON data type
    • Value

    • String

    • Numerical

    • Object

      • Unordered key-value pairs collection
      • Key must be a string surrounded by double quotes
      • Value can be any legal value
    • Array
      • A collection of ordered values
Common methods
import json
    • Dumps (obj, *, Skipkeys=false, ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=None, Separators=none, Default=none, Sort_keys=false, **kw)

      • JSON encoding
    • Dump (obj, FP, *, Skipkeys=false, ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=none, Separators=none, Default=none, Sort_keys=false, **kw)

      • JSON encoding and depositing files
    • Loads (s, *, Encoding=none, Cls=none, Object_hook=none, Parse_float=none, Parse_int=none, Parse_constant=none, object_ Pairs_hook=none, **kw)

      • JSON decoding
    • Load (FP, *, Cls=none, Object_hook=none, Parse_float=none, Parse_int=none, Parse_constant=none, Object_pairs_hook=none , **kw)
      • JSON decoding, reading data from a file
Messagepack Module
    • Similar to JSON, but the same data takes up less space than JSON
Installation
    • $ pip Install Msgpack-python
Common methods
import msgpack
  • PACKB (o, **kwargs) <=> dumps

    • Serializes the object. Provides dumps for compatibility with Pickle and JSON
  • UNPACKB (Packed, object_hook=none, List_hook=none, bool use_list=1, Encoding=none, unicode_errors= ' strict ', object_ Pairs_hook=none, Ext_hook=exttype, py_ssize_t max_str_len=2147483647, py_ssize_t max_bin_len=2147483647, Py_ssize_t max_array_len=2147483647, py_ssize_t max_map_len=2147483647, py_ssize_t max_ext_len=2147483647) <==> loads

    • Deserializes the object. Provides loads to compatible
  • Pack (O, Stream, **kwargs) <==> dump

    • The serialized object is saved to a file object. Provides a dump to be compatible with
  • Unpack (stream, Object_hook=none, List_hook=none, bool use_list=1, Encoding=none, unicode_errors= ' strict ', object_ Pairs_hook=none, Ext_hook=exttype, py_ssize_t max_str_len=2147483647, py_ssize_t max_bin_len=2147483647, Py_ssize_t max_array_len=2147483647, py_ssize_t max_map_len=2147483647, py_ssize_t max_ext_len=2147483647) <==> load
    • Deserializes the image to a file object. Provides load to be compatible with

Python Sixth Week Study notes (1)

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.