Python 代码片段整理

时间:2024-07-19 16:05:50

1、numpy.random.shuffle(x)

 import numpy as np

 x = []
for i in range(10):
x.append(i) print(x)
np.random.shuffle(x)
print (x)

2、python 下载文件 urllib

 import urllib.request
import os
import sys
import tarfile def download_and_uncompress_tarball(tarball_url, dataset_dir):
"""Downloads the `tarball_url` and uncompresses it locally.
Args:
tarball_url: The URL of a tarball file.
dataset_dir: The directory where the temporary files are stored.
"""
filename = tarball_url.split('/')[-1]
filepath = os.path.join(dataset_dir, filename) def _progress(count, block_size, total_size):
sys.stdout.write('\r>> Downloading %s %.1f%%' % (
filename, float(count * block_size) / float(total_size) * 100.0))
sys.stdout.flush()
filepath, _ = urllib.request.urlretrieve(tarball_url, filepath, _progress)
print()
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dataset_dir) DATA_URL = 'http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
DATA_DIR = 'data' download_and_uncompress_tarball(DATA_URL, DATA_DIR)

a).使用 urllib.request.urlretrieve下载

b).内嵌入了一个 回调函数 _progress,显示其进度

c).使用 tar.file.open 进行解压