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}