Module 2: serialization module and collections module

Keywords: Python JSON Excel encoding

I. serialization module

  • json module
'''
Serialization: converting data types from python or other languages to string types
 json module: is a serialization module.
    json:
        Is a "third party" special data format.

        You can use the python data type -- json data format -- string -- File

        To use python data in other languages:
            In the file ----- string ----- json data format ----- data types of other languages.

        Note: in json, all strings are double quotes

        #Tuples are special:
        If a tuple in python is converted to json data, the tuple -- > list will be internally changed (variable type)

        #set cannot be converted to json data

Why use json:
    -In order to share data between different languages.

    PS: because the data types of different languages are different, but they can look the same,
    For example, python can't directly use data types of other languages,
    The data types of other languages must be converted to json data format,
    After python obtains json data, it can convert json to pyton's data type.

How to use:
    import json

    - json.dumps: 
    json.dumps(), f = open() --> f.write()
        #Serialization: in python data type -- json -- string -- json file

    - json.loads:
    f = open(), str = f.read(),  json.loads(str)
        #Deserialization: string, json, python or other language data types in json files

    -json.dump(): serialization: in python data type -- json -- string -- json file
        -Internal implementation f.write()

    -json.load(): ා deserialization: in json file -- "string --" json -- "python or other language data types
        -Internal implementation f.read()

    -dump, load: easier to use

Note: when saving json data, use. json as the suffix of the file
'''

import json

List of items
 list = ['sub peak', 'Xiaoming', 'Reba']
#dumps is serialization: String python data (or other languages) -- > JSON data format
 #Ensure ABCD default is True
json_str = json.dumps(list)
print(json_str)
>>>["\u4e9a\u5cf0", "\u5c0f\u660e", "\u70ed\u5df4"]
#
#
#This way, we can realize the original appearance
json_str = json.dumps(list, ensure_ascii=False)
print(json_str)
>>>["Yafeng", "Xiaoming", "Reba"]
print(type(json_str))
>>><class 'str'>
#
#
#loads is deserialization: String - > JSON data format - > Python data (other languages)
python_data = json.loads(json_str)
print(python_data)
>>>['sub peak', 'Xiaoming', 'Reba']
print(type(python_data))
>>><class 'list'>


Yuan Zu
 tuple1 = ('sub peak', 'Xiaoming', 'Reba')
json_str = json.dumps(tuple1)
print(json_str)
>>>["\u4e9a\u5cf0", "\u5c0f\u660e", "\u70ed\u5df4"]
json_str = json.dumps(tuple1, ensure_ascii=False)
print(json_str)
>>>["Yafeng", "Xiaoming", "Reba"] in json "" represents string
print(type(json_str))
>>><class 'str'>
#
python_data = json.loads(json_str)
print(python_data)
>>>['sub peak', 'Xiaoming', 'Reba']
print(type(python_data))
>>><class 'list'>
#
#
Dictionary of Chinese characters
dic = {
     'name': 'yafeng',
     'age': '18'
 }
json_str = json.dumps(dic)
print(json_str)
>>>{"name": "yafeng", "age": "18"}
print(type(json_str))
>>><class 'str'>
json_str = json.dumps(dic, ensure_ascii=False)
print(json_str)
>>>{"name": "yafeng", "age": "18"}
#
#
python_obj = json.loads(json_str)
print(python_data)
>>>['sub peak', 'Xiaoming', 'Reba']
print(type(python_data))
>>>['sub peak', 'Xiaoming', 'Reba']
#
#
#Note that sets cannot be serialized into json****
set1 = {1, 2, 3, 4}
json_str = json.dumps(set1)
print(json_str)
>>>TypeError: Object of type set is not JSON
 >>>Serializable (serializable)


#Registration function
# def register():
#username = input('Please enter your name: '). strip()
#password = input('Please enter your password: '). strip()
#re_password = input('Please confirm your password: '). strip()
#     if password == re_password:
#         user_dic = {
#             'name': 'username',
#             'pwd': 'password'
#         }
#         json_str = json.dumps(user_dic, ensure_ascii=False)
#
#Start writing files
 #When saving json data, use. json as the suffix
#         with open('user.json','w',encoding='utf-8')as f:
#             f.write(json_str)
#
# register()


#Usage of dump and load
import json

user_dic = {
    'name': 'yafeng',
    'age': 18
}
with open('user1.json', 'w', encoding='utf-8')as f:
    Json.dump (user ﹣ DIC, f) ﹣ first open, write

with open('user2.json', 'w', encoding='utf-8')as f:
    json.dump(user_dic, f)

with open('user2.json', 'r', encoding='utf-8')as f:
    user_dic = json.load(f)
    print(user_dic)
    print(type(user_dic))
    #>>>{'name': 'yafeng', 'age': 18}
    #>>><class 'dict'>

  • pickle module
'''
pickle Modular:
    pickle It is a python The native serialization module.

    //Advantage:
        - Can support python All data types in(Set is also OK.)
        - Can be saved directly "bytes type" Data, pickle Faster access

    //Disadvantages: (fatal disadvantages)
        - Can only support python To use, not cross platform
'''
import pickle

dic = {
    'name': 'yafeng',
    'age': '18',
    'love': 'Hot bar'
}

#Convert to serialized dump(bytes)
with open('yafeng.pickle', 'wb')as f:
    pickle.dump(dic, f)

#Read to original
with open('yafeng.pickle', 'rb')as f:
    python_dic = pickle.load(f)#Note that load can only load one object
    print(python_dic)
    #>>>{'name': 'Yafeng', 'age': '18', 'love': 'Reba'}
    print(type(python_dic))
    #>>><class 'dict'>


set1 = {'yafeng', '18', 'Hot bar'}
with open('1.pickle', 'wb')as f:
    pickle.dump(set1, f)

with open('1.pickle', 'rb')as f:
    python_set = pickle.load(f)
    print(python_set)
    #>>>{'18', 'Reba', 'yafeng'}
    print(type(python_set))
    #>>><class 'set'>


II. collections module

  • named tuple
'''
- python Default eight data:
            - integer
            - float
            - Character string
            - Dictionaries
            - tuple
            - list
            - aggregate
            - Boer
collections Modular:
    - Provide some python The eight data types are "data types other than".


    - named tuple:
        //A named tuple is just a name.
        //Application scenario:
            - coordinate
            -

        from collections import namedtuple

    - Ordered dictionary:
        - python Chinese dictionary is unordered by default

        - collections An ordered dictionary is provided in

        from collections import OrderedDict

'''

#named tuple

from collections import namedtuple

#The incoming iteratable objects are ordered and indexed (such as list, tuple, string)

#Application 1: coordinates
#Change 'coordinate' to the name of 'object'
point = namedtuple('coordinate', ['x', 'y'])#In this case, the second parameter can be passed to the iterative object
point = namedtuple('coordinate', ('x', 'y'))#In this case, the second parameter can be passed to the iterative object
point = namedtuple('coordinate', 'x y')#At this point, the second parameter can be passed to the iteratable object. Note that each element in the string must be separated by a space

#The number of parameters to be passed should be the same as the number of the second parameter
p = point(1, 2)
print(p)
#Coordinates (x=1, y=2)
print(type(p))
#< class' > main.


#Application 2. Playing cards
#Get playing card object
card = namedtuple('Poker', ['color', 'number'])#It's a poker player

#To produce a playing card from a playing object
red_A = card('❤', 'A')
print(red_A)
#>>>Playing cards (color = '❤', number='A ')
print(type(red_A))
#>>>< class'; main poker '>

black_k = card('♠', 'K')
print(black_k)
#>>>Playing cards (color = '♠', number='K ')
print(type(black_k))
#>>>< class'; main poker '>

#Application 3. Movie information
p = namedtuple('Island country', ['city', 'movie_type', 'name'])
movie_info = p('Tokyo', 'Love action movie', 'cang Teacher')
print(movie_info)
#>>>city = 'Tokyo', movie_type = 'love action film', name='cang teacher')
print(type(movie_info))
#>>>< class' [main island country] >


#2. Ordered dictionary
#The default dictionary in python is unordered******

dic = {
    'name': 'yafeng',
    'age': 18,
    'love': 'Hot bar'
}
print(dic)
#>>>{'name': 'Yafeng', 'age': 18, 'love': 'Reba'}
print(type(dic))
#>>><class 'dict'>
for i in dic:
    print(i)
    #>>>name
    #>>>age
    #>>>love


#Ordered dictionary
from collections import OrderedDict

order_dic = OrderedDict(dic)
print(order_dic)
#>>>Ordereddict ([('name ',' Yafeng '), ('age', 18), ('Love ',' Reba ')])
print(type(order_dic))
#>>><class 'collections.OrderedDict'>

print(order_dic.get('love'))
#"Hot bar"
print(order_dic['love'])
#"Hot bar"

for i in order_dic:
    print(i)
    #name
    #age
    #love

III. openpyxl module

  • Openpyxl (a module that can operate on ecxel tables)
# '''
# openpyxl module: third party module
#     -Modules that can operate on the excel table
#
#     - Download:
#         pip install openpyxl
#
#     -Excel version:
#         2003 before:
#             excle name.xls
#
#         2003 after that:
#             excle name.xlsx
#
#     -Tsinghua source: https://pypi.tuna.tsinghua.edu.cn/simple
#
#     -To configure a permanent third-party source:
#         D:C:\Users \ Zhan Yafeng \ appdata \ local \ programs \ Python \ Python 37 \ lib \ site packages \ PIP \ \
# '''

# #Write data
# from openpyxl import Workbook
#
# #Get Excel file object
# wb_obj = Workbook()
#
# wb1 = wb_obj.create_sheet('sub peak cowhide 1 ', 1)
# WB2 = WB obj. Create sheet ('sub peak cowhide 2 ', 2)
#
# #Modify the name of the worksheet, change 'Yafeng cowhide 2' to - > 'Yafeng 666'
# print(wb2.title)
# wb2.title = 'sub peak 666'
# print(wb2.title)
#
#
# #Add values to the first sheet
# #wb1 ['table location in workbook']
# wb1['A10'] = 123
# wb1['B10'] = 666
# wb1['C10'] = '=SUM(A10:B10)'
#
# #Generate Excel table
# wb_obj.save('sub peak cowhide 1.xlsx ')
# print('excel table generated successfully ')


# #Read data
# from openpyxl import load_workbook
# WB ﹣ obj = load ﹣ Workbook ('sub peak 1.xlsx ')
# print(wb_obj)
#
# #WB ABCD obj ['table name']
# wb1 = wb_obj ['sub peak cowhide 1']
# print(wb1['A10'].value)
# wb1['A10'] = 666
# print(wb1['A10'].value)


#Write 100 pieces of data in batch
from openpyxl import Workbook

wb_obj = Workbook()

wb1 = wb_obj.create_sheet('Worksheet 1')

#wb1 ['table position'] = 'corresponding value'
# n = 1
# for line in range(100):
#     wb1['A%s' %n] = line + 1
#     n += 1


#Hypothesis: a dictionary of 10000 pieces of data
dic = {
    'name': 'Asian peak',
    'age': 18,
    'love': 'Hot bar'
}

n = 1
for key, value in dic.items():
    wb1['A%s' %n] = key
    wb1['B%s' %n] = value
    n += 1

wb_obj.save('Bulk inserted data.xlsx')

Posted by richardbotw on Mon, 18 Nov 2019 01:47:32 -0800