程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python note 21 serialization and deserialization of objects & reading and writing of Excel

編輯:Python

One 、 File read and write

1. Serialization and deserialization of objects

【 Interview questions 】 Briefly describe the serialization and deserialization of objects

object serialization : take Python Any object in the is persisted to disk

Deserialization of objects : Read out the objects on the disk , Convert to a Python object

stay Python in , Modules that can serialize and deserialize objects :pickle and json

1.1pickle modular

import pickle
# 1.
def write_file1():
with open(r"file1.txt",'a',encoding='utf-8') as f:
# Be careful : Writing normal text files , The argument must be a string , It can't be any other type
f.write("abc")
# TypeError: write() argument must be str, not list
# f.write([34,56,7])
# 2.
"""
Be careful :
1. Serialization and deserialization of objects , Objects operate in bytes , So use wb or rb Mode on
2. object serialization : It is equivalent to writing the object to the local file ,pickle.dump()
3. Deserialization of objects : It is equivalent to reading the object from the local file ,pickle.load()
"""
class Person(object):
def __init__(self,name,age):
self.name = name
self.age = age
def show(self):
print(f" full name :{self.name}, Age :{self.age}")
# a. serialize
"""
pickle.dump(obj,file)
obj: Need to be serialized Python object ,Python Everything is an object
file: File objects to be written
"""
def write_file2():
with open(r"file2.txt", 'wb') as f:
# pickle.dump("hello",f)
# pickle.dump([34,6,8,89],f)
p = Person(" Zhang San ",10)
pickle.dump(p, f)
# b. Deserialization
"""
pickle.load(file)
file: File objects to read
"""
def read_file2():
with open(r"file2.txt", 'rb') as f:
r = pickle.load(f)
print(r,type(r))
r.show()
if __name__ == '__main__':
write_file2()
read_file2()

1.2json modular

JSON(JavaScript Object Notation, JS Object shorthand ) Is a lightweight data exchange format . Easy to read and write . At the same time, it is also easy for machine analysis and generation

benefits :

a. Data can be exchanged between different operating systems
b. Data can be exchanged between different programs
c. Data can be exchanged between different programming languages

JSON and Python Data type comparison in

Python type JSON type explain
dict object Dictionaries / object
list array list / Array
str string character string
int/float number Numbers
True/False true/false Boolean value
None null Null value

JSON Format :

{
"name": " China ",
"province": [{
"name": " heilongjiang ",
"cities": {
"city": [" Harbin ", " Daqing "]
}
}, {
"name": " guangdong ",
"cities": {
"city": [" Guangzhou ", " Shenzhen ", " zhuhai "]
}
}, {
"name": " Taiwan ",
"cities": {
"city": [" Taipei ", " Kaohsiung "]
}
}, {
"name": " xinjiang ",
"cities": {
"city": [" urumqi "]
}
}]
}
import json
info_dict = {
"name": " China ",
"province": [{
"name": " heilongjiang ",
"cities": {
"city": [" Harbin ", " Daqing "]
}
}, {
"name": " guangdong ",
"cities": {
"city": [" Guangzhou ", " Shenzhen ", " zhuhai "]
}
}, {
"name": " Taiwan ",
"cities": {
"city": [" Taipei ", " Kaohsiung "]
}
}, {
"name": " xinjiang ",
"cities": {
"city": [" urumqi "]
}
}]
}
print(info_dict)
print(type(info_dict))
# 1. serialize [Serialize]
"""
json.dump(obj,file): take python The dictionary or list object in is serialized as json character string , Then write the string to the specified file
obj: Need to be serialized Python object , Common string , Numbers , Boolean value , Dictionaries or lists
file: File objects to be written
json.dumps(obj): take python The dictionary or list object in is serialized as json character string
"""
# a
# Be careful : If Python The content of the dictionary or list of contains Chinese , It will be encoded by default , If normal display is required , Set up ensure_ascii=False
r1 = json.dumps(info_dict,ensure_ascii=False)
print(r1)
print(type(r1))
# b.
with open(r"file3.json",'w',encoding='utf-8') as f:
json.dump(info_dict,f,ensure_ascii=False)
# 2. Deserialization [Deserialize]
"""
json.load(file): The... Contained in the file object will be specified json The string is deserialized , Turn into Python List or dictionary objects in
json.loads(): take JSON The string is deserialized , Turn into Python List or dictionary objects in
"""
# a.
r2 = json.loads(r1)
print(r2)
print(type(r2))
# b.
with open(r"file3.json",'r',encoding='utf-8') as f:
r3 = json.load(f)
print(r3)
print(type(r3))

2.Excel Reading and writing

Python The modules in are divided into three categories :

a. System module : direct import xxx
b. Custom module
c. Third-party module , Must be installed first , And then use

operation excel Third party modules for :

1.pandas: It is a module for data processing and data analysis , It can be used to deal with excel

2.xlrd,xlwt: You can do basic excel The operation of ,xlrd Responsible for reading ,xlwt Responsible for writing ,xlutils Be responsible for assistance and connection , Aiming at .xls

3.xlwings: Not only can the content be read and written , You can also modify the cell format

4.xlsxwriter: For writing text , Numbers , The formula , You can also format cells , picture , Chart , Automatic filtering and other features , Aiming at .xlsx

5.openpyxl: From Workbook —》 Worksheet ——》 Cell pattern pairs .xlsx Read and write files , modify , You can adjust the style

Installation process of third-party modules :

a. stay cmd in , perform pip install xxxx, default pip Source abroad , Slow download
b. If modified pip Mirror source , Can be pip The image source is modified to domestic , Carry out orders :pip install xxxx -i Mirror source
Common domestic image sources are as follows :
(1) Alibaba cloud http://mirrors.aliyun.com/pypi/simple/
(2) douban http://pypi.douban.com/simple/
(3) Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/
(4) University of science and technology of China http://pypi.mirrors.ustc.edu.cn/simple/
(5) Huazhong University of science and technology http://pypi.hustunique.com/
(6)Python official https://pypi.python.org/simple
c. If you want to uninstall a third-party module , execute pip uninstall xxx
# excel: workbook ----》 Worksheet ----》 That's ok ----》 Cell
import xlrd,xlwt
# 1.xlrd: Read / obtain : understand
# a. Open the workbook
workbook = xlrd.open_workbook(r" League tables .xls")
print(workbook)
# b. Get all the worksheets
print(workbook.sheet_names()) #['1 class ', '2 class ']
# c. Gets the specified from the index sheet object
sheet = workbook.sheet_by_index(0)
print(sheet)
# d. Get the specified according to the worksheet name sheet object
sheet = workbook.sheet_by_name('1 class ')
print(sheet)
print(sheet.nrows) # That's ok
print(sheet.ncols) # Column
# e. Get the cells of the specified worksheet
cell = sheet.cell(1,0)
print(cell)
print(cell.value) # Get the text value of the cell
# 2.xlwt: master , Later, it is used to store data in the crawler
# a. Create Workbook
workbook = xlwt.Workbook()
print(workbook)
# b. Add a worksheet
sheet = workbook.add_sheet("sheet1")
# Be careful : towards excel When writing content in , The file must be closed , Both rows and columns are from 0 Start calculated
# c. Write header , The first 0 Xing di 0~2 Column
headers = [' full name ',' Age ',' Address ']
for index,header in enumerate(headers):
# sheet.write(row,col,value)
sheet.write(0,index,header)
# d. Write the text , The first 1~4 Column number 1 0~2 Column
data = [
[' Zhang San ',10,' Beijing '],
['aaa',10,'shanghai'],
[' Li Si ',14,'guangzhou'],
['tom',10,' Hangzhou ']
]
for row in range(len(data)):
for col in range(len(data[row])):
sheet.write(row + 1,col,data[row][col])
# e. Save workbook , Be careful : The file format is saved as xxx.xls
workbook.save('info.xls')

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved