from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
with PyCallGraph(output=GraphvizOutput()):
# 需要绘制流程图的代码,可以是函数
# ... ...
# ...省略大部分代码...
if __name__ == '__main__':
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
with PyCallGraph(output=GraphvizOutput()):
main()
>>> 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}
# 随意插入新的词语
for i in range(1000):
words.add('生日', numpy.random.randint(1, 5))
words.add('快乐', numpy.random.randint(1, 5))
words.add(name, numpy.random.randint(1, 5))
然后我们需要对图片进行一些处理,现在网络上的图片很多都包含一些杂色,因此需要把这些杂色去掉:
def transform_format(val):
# 用于去除杂色
if val[0] > 245 and val[1] > 245 and val[2] > 245:
val[0] = val[1] = val[2] = 255
return val
else:
return val
然后引入图片,去除杂色:
# 设定图片
bimg = imread(file)
for color in range(len(bimg)):
bimg[color] = list(map(transform_format, bimg[color]))
wordcloud = WordCloud(
background_color='white', mask=bimg,
font_path='simhei.ttf'
).generate_from_frequencies(words)