The road to wudao-the five-day post of the refining stage

Source: Internet
Author: User

The road to wudao-the five-day post of the refining stage

Serialization: changes an object from memory to a stored and transmitted process, and vice versa.

The standard format of objects transmitted between different programming languages is json (and xml). However, in python, json can only serialize simple objects. in complicated cases, pickle is generally used (it may not be deserialized)

For json, double quotation marks are recommended for dictionary serialization. Important Methods: dumps () and dump ()-> serialization, loads () and load ()-> deserialization,

Dump converts a python object to a json object to generate a fp file stream, while dumps generates a string, which is the same as load and loads.

Import json, pickle

Dict1 = {"name": "abc", "score": 77}

With open('test.txt ', 'w') as f:

F. write (json. dumps (dict1) # Same as pickle, here is the write

Json. dump (dict1, f) # Same as pickle, write

Json. loads (f. read (), json. load (f) read

Module: essentially a. py file. To implement some functions (variables, functions, classes), the file name is test. py. The module name is test.

Package: essentially a directory or folder, which must have a _ init _. py file (with some modules)

Introduction:

Import x, y, z as m references multiple modules, and as takes the alias

Import test to test. the code of the py file is interpreted once and assigned to test (call the variable: test. variable name. Call the function: test. function Name (), the same as from test import * (paste the code here. We do not recommend this to avoid repeated variable functions)

From test import x (variables or methods under test. aliases can be used as if duplicate names are prevented)

The essence of the import package is to execute the _ init _. py file under it.

 

Sometimes the import module is not in the same directory or under it:

Imporyt OS, sys

_ File _ relative path of the current program

OS. path. abspath (_ file _) absolute path of the current program

OS. path. dirname (_ file _) remove the last path

Sys. path. append (url) to set environment variables.

 

 

  

 

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.