Learning summary on October 11
1, Exercise: importing prime numbers within 100 into a file
def is_prime(num:int) -> bool: """ Judge whether a positive integer is a prime number :param num: positive integer :return: Prime return True,Otherwise return False """ for i in range(2,sqrt(num)+1): if num%i == 0: return False return True with open('prime.txt','w') as file: for n in range(2,100): if is_prime(n): print(n)
2, Serialization and deserialization of objects
1. serialization
Turn an object (Dictionary, list, etc.) into a string (str) or a byte string (bytes binary data)
2. deserialization
Restore an object (Dictionary, list, etc.) from a byte string or string
3.JSON serialization
1.Python's standard library has a json/pickle module, which can support us to serialize and deserialize
2.Json
(1) JSON - > JavaScript object notation - > literal syntax for creating objects in JavaScript language
This data format is also very suitable for transmitting data between two systems (especially heterogeneous systems) (because it is plain text), so when we talk about JSON today, we mostly regard it as a data exchange format.
3. The dictionary in Python is very similar to the JSON format, so we can write the dictionary into a file by converting it into a string in JSON format.
JSON serialization is universal and readable by all programming languages; String (txt file)
4.json.dumps/json.dump
(1) JSON. Dumps (variable, file = file name): in two steps, dumps first turns the object into a string, and then writes it into file
Content = json.dumps (variable)
file.write(content)
(3) JSON. Dump (variable, fp = file name): combine the two steps of dumps into one step
import json person = { 'name': 'Haoyu', 'age': 25, 'sex': True, 'friends': ['Zhao Yun', 'ma chao', 'Xin Qiji'], 'car': { 'brand': 'QQ', 'max_speed': 120 } } with open('person.txt', 'w') as file: # serialize # content = json.dumps(person) # file.write(content) # dumps first changes the object to a string # print(json.dumps(person), file=file) json.dump(person, fp=file) # You can specify parameters
4. Deserialization of JSON
-
JSON: read the JSON format data in the file and restore it to a dictionary object
(1) json.load(fp = file name) (2) json.loads (variable): the variable is the string obtained by reading the file
import json with open('person.txt') as file: # Deserialization: restore string to (Dictionary) # content = file.read() # obj = json.loads(content) # print(obj, type(obj)) # Restore a string read from a file to a dictionary object obj = json.load(fp=file) print(obj) print(type(obj))
3, Network access to data
1. URL
- URL - > Web address - > Uniform Resource Locator - > a symbol that uniquely identifies a (Network) resource
- URI - > uniform resource identifier - > URL + urn
- Protocol - > specifications and standards - > network protocol - > specifications and standards to be observed by both parties communicating through the website
- HTTP(S) - > Hypertext Transfer Protocol (S: security environment) - > request response protocol
2. Third party Library
You can use Python's package management tool pip to install and manage third-party libraries and third-party tools
Modify the download source of pip to the domestic image website (Douban image is recommended)
pip config set global.index-url https://pypi.doubanio.com/simple
Find a third-party library: pip serach requests
Install a third-party library: pip install requests
Uninstall third-party libraries: pip uninstall requests
Update the third-party library: pip install -U requests
3. Use of third-party library requests
Using the third-party library requests, it is very convenient to access network resources through URL
requests.get (url = 'web address', params = {parameter 1, parameter 2,...}): the get function will send a request to the web server through the URL you specify, and the index will return a response object
Response object. json(): the url request returns a json file, which is converted into a dictionary with. json
JSON. Loads (response object * *. text * *): the string file is converted back into a dictionary
4, Excel file operation
import openpyxl
Create an Excel Workbook: workbook = openpyxl.Workbook()
Get the default worksheet: sheet = workbook.active
Add header: sheet.append (list, tuple, dictionary, etc.)
Save Workbook: workbook.save('filename. xlsx ')
import openpyxl # Create an Excel Workbook workbook = openpyxl.Workbook() # Get default worksheet sheet = workbook.active # Add header sheet.append(('full name','chinese','mathematics','English')) sheet.append(('Wang Boyang',50,60,70)) sheet.append(('Li Xiao',50,60,70)) sheet.append(('Wang Haoyu',50,60,70)) sheet.cell(5,1,'Daiji') # Save Workbook workbook.save('Student examination transcript.xlsx') import openpyxl # Create an Excel Workbook workbook = openpyxl.Workbook() # Get default worksheet sheet = workbook.active # Add header sheet.append(('full name','Age','Telephone number')) sheet.append(('wby','18','113')) sheet.append(('lx','23','116')) # Save Workbook workbook.save('Student information.xlsx')
from datetime import datetime import openpyxl import requests import json workbook = openpyxl.Workbook() sheet = workbook.active sheet.append(('title','url','source')) for page in range(1,6): resp = requests.get( url='http://api.tianapi.com/topnews/index', params={ 'key':'e8c5524dd2a365f20908ced735f8e480', 'page':page, 'num':20 } ) result = resp.json() # print(result) for news_dict in result['newslist']: sheet.append((news_dict['title'],news_dict['url'],news_dict['source'])) current=datetime.now() workbook.save(f'News data_{current.year}{current.month:0>2d}{current.day:0>2d}.xlsx')