Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.
df1 = pd.DataFrame({"A": [1, 2, 3]})
df2 = pd.DataFrame({"B": [2, 0, 3]})
df3 = pd.DataFrame({"C": [3, 2, 3]})
with pd.ExcelWriter("path_to_file.xlsx", engine="openpyxl") as writer:
df1.to_excel(writer, sheet_name='Sheet1')
df2.to_excel(writer, sheet_name="页2")
with pd.ExcelWriter("path_to_file.xlsx", engine="openpyxl", mode="a") as writer:
df3.to_excel(writer, sheet_name="Sheet3", index=False)
test = pd.read_excel("path_to_file.xlsx", sheet_name=[0, "Sheet3"])
print(type(test)) # <class 'dict'>
print(test.keys()) # dict_keys([0, 'Sheet3'])
# 直接使用xlrd包
import xlrd
wb = xlrd.open_workbook("path_to_file.xlsx")
sheet = wb.sheet_by_index(0)
sheet = wb.sheet_by_name("Sheet3")
# <class 'xlrd.book.Book'> <class 'xlrd.sheet.Sheet'> 4 1
print(type(wb), type(sheet), sheet.nrows, sheet.ncols)
for i in range(sheet.nrows):
for j in range(sheet.ncols):
print(sheet.cell_value(i, j), end=" ")
print()
# Writing to an excel sheet using Python 3.x. or earlier
import xlwt as xw
# Workbook is created
wb = xw.Workbook()
# add_sheet is used to create sheet.
sheet1 = wb.add_sheet('Sheet 1')
# Specifying style of the elements
style_value1= xw.easyxf('font: bold 1')
style_value2 = xw.easyxf('font: bold 1, color blue;')
# Input data into rows
sheet1.write(1, 0, 'Code Speedy', style_value1)
sheet1.write(2, 0, 'Sarque Ahamed Mollick', style_value2)
# Input data into columns
sheet1.write(0, 1, 'Position')
sheet1.write(0, 2, 'No of Posts')
# 似乎不能写为以.xlsx为后缀的文件(运行不报错, 但使用Excel2019打不开)
wb.save('xlwt codespeedy.xls') # .xls文件能用Excel2019打开
x = pd.ExcelFile(r"C:\Users\chenbx\Desktop\调优\默认值.xlsm")
sheet_names = x.sheet_names
y = pd.ExcelWriter(r"C:\Users\chenbx\Desktop\调优\默认值.xlsx")
for sheet in sheet_names:
df = pd.read_excel(r"C:\Users\chenbx\Desktop\调优\默认值.xlsm", sheet_name=sheet)
df.to_excel(y, sheet_name=sheet)
y.save()
# 解压文件
import zipfile
zipname = r'D:\work0126\aa.zip'
out_dir = r"C:\work0126"
pswd = '12345'
with zipfile.ZipFile(zipname) as file:
# password you pass must be in the bytes you converted 'str' into 'bytes'
file.extractall(path=out_dir, pwd = bytes(pswd, 'utf-8'))
# 打包为zip
# res = requests.get("https://raw.githubusercontent.com/explosion/spacy-models/master/shortcuts-v2.json")
# res.json()
# 确定下载的模型版本
res = requests.get("https://raw.githubusercontent.com/explosion/spacy-models/master/compatibility.json")
version = res.json()["spacy"]["2.1.9"]['en_core_web_sm'] # 2.1.0为2.1.9版本相匹配的en_core_web_sm模型
# 最后实际调用了
# python -m pip install --no-cache-dir --no-deps <download-url>
download_url = "https://github.com/explosion/spacy-models/releases/download/"
+ "en_core_web_sm-2.1.0/en_core_web_sm-2.1.0.tar.gz#egg=en_core_web_sm==2.1.0"
m = "en_core_web_sm"
v = "2.1.0"
format = "{m}-{v}/{m}-{v}.tar.gz#egg={m}=={v}"
huggingface transformers
基本使用
模型下载目录
Caching models
This library provides pretrained models that will be downloaded and cached locally. Unless you specify a location with cache_dir=... when you use methods like from_pretrained, these models will automatically be downloaded in the folder given by the shell environment variable TRANSFORMERS_CACHE. The default value for it will be the Hugging Face cache home followed by /transformers/. This is (by order of priority):
So if you don’t have any specific environment variable set, the cache directory will be at ~/.cache/huggingface/transformers/.
Note: If you have set a shell environment variable for one of the predecessors of this library (PYTORCH_TRANSFORMERS_CACHE or PYTORCH_PRETRAINED_BERT_CACHE), those will be used if there is no shell environment variable for TRANSFORMERS_CACHE.
开源模型
使用方法:
# 前三行参照模型地址
# https://huggingface.co/Helsinki-NLP/opus-mt-en-zh/tree/main
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-zh")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-en-zh")
# 后面三行参照transformers文档
# https://huggingface.co/transformers/task_summary.html#translation
inputs = tokenizer.encode("translate English to German: Hugging Face is a technology company based in New York and Paris", return_tensors="pt")
outputs = model.generate(inputs, max_length=40, num_beams=4, early_stopping=True)
print(tokenizer.decode(outputs[0]))
from rediscluster import RedisCluster # 旧版, redis-py-cluster 2.x, 依赖于redis 3.x
from redis.cluster import RedisCluster # redis 4.x
In the upstream package redis-py that this librar extends, they have since version * 4.1.0 (Dec 26, 2021) ported in this code base into the main branch.
The cluster client is based on Grokzen’s redis-py-cluster, has added bug fixes, and now supersedes that library. Support for these changes is thanks to his contributions
def int2str(x):
return tuple(str(i) if isinstance(i, int) else int2str(i) for i in x)
x = ((1, 2), 1, 3)
int2str(x) # 输出(('1', '2'), '1', '3')
# 一个综合的例子
from functools import wraps
def to_string(func):
@wraps(func)
def wrapper(*args, **kwargs):
def int2str(x):
return tuple([str(i) if isinstance(i, int) else int2str(i) for i in x])
return int2str(func(*args, **kwargs))
return wrapper
@to_string
def f():
"""asd"""
return ((1, 2), 1, 3)
f.__doc__
# for else字句, 若正常结束, 则执行else语句
for n in range(2, 8):
for x in range(2, n):
if n % x == 0:
print( n, 'equals', x, '*', n/x)
break
else:
# loop fell through without finding a factor
print(n, 'is a prime number')