def decode_result(system, result):
"""
解码密码
Arguments:
system {str} -- [系统类型]
result {str} -- [输出]
Returns:
[str] -- [解码后的密码]
Author: Python 实用宝典
"""
if system == "windows":
# cmd命令得到的结果是bytes型,需要decode
result = result.decode("gb2312")
result = result.strip('\r|\n')
if result != "":
result = result.replace(" ", "")
result = result[result.find(":") + 1:]
result = result[result.find("=") + 1:]
return result
>>> print(data['first_name'])
['John', 'George', 'Henry']
>>> print(data)
First Name|Last Name |age
----------|----------|---
John |Adams |90
George |Washington|67
Henry |Ford |83
>>> data.get_col(1)
['Adams', 'Washington', 'Ford']
删除记录
>>> del data[1]
>>> print(data)
First Name|Last Name|age
----------|---------|---
John |Adams |90
Henry |Ford |83
>>> data.yaml
'- {First Name: John, Last Name: Adams, age: 90}\n- {First Name: Henry, Last Name: Ford, age: 83}\n'
>>> print(data.yaml)
- {First Name: John, Last Name: Adams, age: 90}
- {First Name: Henry, Last Name: Ford, age: 83}
>> f = open('data.yaml', 'w', encoding='utf-8')
>> f.write(data.yaml)
>> f.close()
excel
>>> with open('people.xls', 'wb') as f:
... f.write(data.xls)
注意要以二进制形式打开文件
dbf
>>> with open('people.dbf', 'wb') as f:
... f.write(data.dbf)
高级使用
动态列
可以将一个函数指定给Dataset对象
import random
def random_grade(row):
"""Returns a random integer for entry."""
return (random.randint(60,100)/100.0)
data.append_col(random_grade, header='Grade')
>>> data.yaml
- {Age: 22, First Name: Kenneth, Grade: 0.6, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Grade: 0.75, Last Name: Monke}
函数的参数row传入的是每一行记录,所以可以根据传入的记录进行更一步的计算:
def guess_gender(row):
"""Calculates gender of given student data row."""
m_names = ('Kenneth', 'Mike', 'Yuri')
f_names = ('Bessie', 'Samantha', 'Heather')
name = row[0]
if name in m_names:
return 'Male'
elif name in f_names:
return 'Female'
else:
return 'Unknown'
>>> data.yaml
- {Age: 22, First Name: Kenneth, Gender: Male, Last Name: Reitz}
- {Age: 20, First Name: Bessie, Gender: Female, Last Name: Monke}
import matplotlib.pyplot as plt
import librosa.display
import numpy as np
from pydub import AudioSegment
# 1秒=1000毫秒
SECOND = 1000
# 音乐文件
AUDIO_PATH = 'Fenn.mp3'
def split_music(begin, end, filepath):
# 导入音乐
song = AudioSegment.from_mp3(filepath)
# 取begin秒到end秒间的片段
song = song[begin*SECOND: end*SECOND]
# 存储为临时文件做备份
temp_path = 'backup/'+filepath
song.export(temp_path)
return temp_path
music, sr = librosa.load(split_music(0, 1, AUDIO_PATH))
# 宽高比为14:5的图
plt.figure(figsize=(14, 5))
librosa.display.waveplot(music, sr=sr)
plt.show()
这下细是细了,但是还是太复杂了,其实我们做频谱的展示,只需要正值即可:
然后我们还可以进一步放大,比如说0.9秒到1秒之间的频谱:
# 放大
n0 = 9000
n1 = 10000
music = np.array([mic for mic in music if mic > 0])
plt.figure(figsize=(14, 5))
plt.plot(music[n0:n1])
plt.grid()
# 显示图
plt.show()
第一步,在注册账号之后,打开 API 密钥管理页面(https://console.cloud.tencent.com/cam/capi)获取到 SecretId 和 SecretKey。
第二步,安装腾讯云的 SDK
pip3 install tencentcloud-sdk-python
人脸属性
在人脸年龄变化 API 中有一个 AgeInfo 参数,它包含了 Age 和 FaceRect 两个属性,其中 FaceRect 属性必须填人脸在照片中基于左上角的 X、Y 坐标和人脸的高度与宽度。所以先要调用人脸检测与分析 API 得到这些数据。
下面的示例图是在百度图片中截取的。
import json import base64 from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.iai.v20200303 import iai_client from tencentcloud.iai.v20200303 import models as models03
import json from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.ft.v20200304 import ft_client, models