Home > Backend Development > Python Tutorial > What are the python code snippets?

What are the python code snippets?

coldplay.xixi
Release: 2020-11-02 15:25:26
Original
3162 people have browsed it

Python code snippet: 1. Requests to crawl images; 2. uuid, the code is [from uuid import uuid4]; 3. Get the cpu memory usage; 4. Convert dictionary to json, the code is [print( json.dumps(dict, ..)】.

What are the python code snippets?

Python code segment:

1, requests to crawl images

import requests
url = 'https://www.baidu.com/img/bd_logo1.png'
data = requests.get(url).content
with open('baidu.png', 'wb') as fb:
    fb.write(data)
Copy after login

2, uuid

from uuid import uuid4
print(uuid4()) # 253b0bda-7f3e-4eeb-9116-b8b4865083b6
Copy after login

3, get cpu memory usage

import psutil
import os
print(os.getpid())
print(psutil.virtual_memory().percent)
print(psutil.cpu_percent(interval=1.0))
# 40462
# 67.2
# 7.1
Copy after login

4. Convert dictionary to json (solve Chinese garbled characters)

import json
dict = {
    'user': 'yubozhi',
    'title': '硬盘不是“背锅侠',
    'time': '2017-11-29'
}
print(json.dumps(dict, ensure_ascii=False))
# {"user": "yubozhi", "title": "硬盘不是“背锅侠", "time": "2017-11-29"}
Copy after login

Related free learning recommendations: python video tutorial

The above is the detailed content of What are the python code snippets?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template