day20 module and import sequence

# Serialization: converting the original dictionaries, lists and other content into a string of process is called serialization. 
Serialization # - turning a string data type
# Sequence - the string
"{ 'K': 'V'}"
# data storage
when transmitted over the network #

# From the data type -> string serialization process
# from a string -> data type during deserialization
# ***** JSON
# the pickle ****
# the shelve ***

# # JSON numeric string dictionary listing tuples
#general serialization format
# only a small part can be converted into a string data type by JSON
# the pickle
data type # all the python can be converted to a string
# sequence of the pickle can understand the contents of only python
# deserialization and partially dependent python code
# The shelve
# serialization handle
# using direct manipulation handle, very convenient

# json dumps serialization method loads deserialize method
# Import JSON
# DIC = (1,2,3,4) {# 'K1': 'V1'}
# Print (type (DIC))
# = str_d json.dumps (DIC) of sequence #
# print(type(str_d),str_d)
# # # '{"kkk":"v"}'
# dic_d = json.loads(str_d) # 反序列化
# print(type(dic_d),dic_d)
#
#
# import json
# #json dump load
# dic = {1:"a",2:'b'}
# f = open('fff','w',encoding='utf-8')
# json.dump(dic,f)
# f.close()
# f = open('fff')
# res = json.load(f)
# f.close()
# print(type(res),res)

# import json
# # json dump load
# dic = {1:"中国",2:'b'}
# f = open('fff','w',encoding='utf-8')
# json.dump(dic,f,ensure_ascii=False)
# json.dump(dic,f,ensure_ascii=False)
# f.close()
# f = open('fff',encoding='utf-8')
# res1 = json.load(f)
# res2 = json.load(f)
# f.close()
# print(type(res1),res1)
# print(type(res2),res2)


# l = [{'k':'111'},{'k2':'111'},{'k3':'111'}]
# f = open('file','w')
# import json
# for dic in l:
# str_dic = json.dumps(dic)
# f.write(str_dic+'\n')
# f.close()

import pickle
# dic = {'k1':'v1','k2':'v2','k3':'v3'}
# str_dic = pickle.dumps(dic)
# print(str_dic) #一串二进制内容
#
# dic2 = pickle.loads(str_dic)
# print(dic2) #字典

import time
struct_time1 = time.localtime(1000000000)
struct_time2 = time.localtime(2000000000)
# f = open('pickle_file','wb')
# pickle.dump(struct_time1,f)
The pickle.dump # (struct_time2, F)
# f.close ()
# = F Open ( 'pickle_file', 'RB')
# = struct_time1 the pickle.load (F)
# = struct_time2 the pickle.load (F)
# Print (struct_time1 .tm_year)
# Print (struct_time2.tm_year)
# f.close ()



# Import The shelve
# shelve.open F = ( 'shelve_file')
# F [ 'Key'] = { 'int': 10, 'a float': 9.5 , 'string': 'Sample data '} # operation directly on the file handle, can be stored in the data
# f.close ()
#
# Import The shelve
# shelve.open F1 = ( 'shelve_file')
# existing F1 = [ 'Key '] # when extracted data is also used to direct access only key, but if the key does not exist being given
# f1.close ()
# Print (existing)


# Import the shelve
# shelve.open F = (' shelve_file ', in Flag = 'r')
# existing = f['key']
# print(existing)
# f.close()
#
# f = shelve.open('shelve_file', flag='r')
# existing2 = f['key']
# f.close()
# print(existing2)

#想要改文件的话必须设置writeback=True
import shelve
f1 = shelve.open('shelve_file')
print(f1['key'])
f1['key']['new_value'] = 'this was not here before'
f1.close()

f2 = shelve.open('shelve_file', writeback=True)
print(f2['key'])
f2['key']['new_value'] = 'this was not here before'
f2.close()

f2 = shelve.open('shelve_file', writeback=True)
print(f2['key'])
# f2['key']['new_value'] = 'this was not here before'
f2.close()

Demo Import 
demo.read ()
Print (demo.money)
# a single module, imported more than once that he only performed once
# files
# Import Demo
# DEF the Read ():
# Print ( 'the Read My FUNC')
# demo.read ()
# Print (demo.money)
# sys.modules start to see if there have been introduced into the
# if not imported, according to sys.path looking for the path to take modules
# find on import
# to create a namespace of this module
# execution file, the file names into the namespace
Import SYS
Print (sys.modules.keys ())
Print (sys.path)

Import AS t Time
Print (t.time ())
# the Oracle
# MySQL
# IF database == 'the Oracle':
# Import the Oracle AS db
# elif database == 'MySQL':
# Import AS MySQL db
## connect to the database db.connect
# login authentication #
# # CRUD
# # Close database

# Import Time, SYS, OS

from SLEEP Time Import
from demo import read
DEF read ():
Print ( 'My read')
read ()
# use from demo import this form of read, and if the re-defined same name read, will overwrite previously imported module, the import demo the problem does not exist in this form
from import Demo Money, Read
# Print (Money)
# Read ()
Money = 200 is
Read ()

from import Time *
# 10 = SLEEP
SLEEP (. 1)

from import Math PI
Print (PI)
PI =. 3
Print (PI)
# when the modules are introduced by the import module name from the variable name if the time defined in the module __all__ = [ 'read', ' read2 ',' login '], the definition of a variable can be called
# import all modules should try to write up
# built-in modules
# expansion module
# custom modules
# import module will not be repeated: sys.moudles
# Import module Where: the sys.path
#import
# import module name
# module variable names and variable names in this document conflicts completely.
# Import module name rename module name as: improved code compatibility
# 1 import module the module 2

#from import
# from module import name variable name
# variable name can be used directly to complete the operation
# If this document have the same variable name conflict
# from import variable name as the module name to rename the variable name
# from module 1 name import variable names, variable names 2
# from the name of the module import *
# variable names all modules are placed in memory
# If this document have the same variable name conflict
# from the module name and import * __all__ a pair of
# without this variable, it will import all the names
# only if all import all the names in the list
# __name__
# there is a variable in the module __name__,
# when we direct the implementation of this module, __ name__ == '__main__'
# when we perform additional module, this module is referenced in other modules, this module __name__ == 'module name'


Guess you like

Origin www.cnblogs.com/Murraya/p/11027074.html