Python
Numpy知识总结
这里记录下numpy常用的一些操作,一些散乱的知识点。
数组和标量之间的运算
就是对数组进行批量的运算
Python
Numpy知识总结
这里记录下numpy常用的一些操作,一些散乱的知识点。
就是对数组进行批量的运算
这里简单介绍下Python中的csv模块,应该蛮常用的。
和csv有关,一定要回合打开文件这类操作有关,这里先看下这个open函数
官方文档:https://docs.python.org/3/library/functions.html#open
open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.
file,就是我们要打开的文件地址;
mode,就是打开的方式,默认是只读文本(’rt’)
newline,和换行符有关,在网上找了个资料:https://www.zhihu.com/question/19751023
换行符看起来有点儿乱,以后如果一段问题了再研究下。
下面,我们先来看看csv
csv.reader(csvfile, dialect=’excel’, **fmtparams)Return a reader object which will iterate over lines in the given csvfile.
我们的csv文件是这样的
|
|
就csv文件来说,会有几个特点,比如字段之间的分隔符,换行符等,我们使用上面的dialect来指定
如果我们,现在将分隔符,替换为^
我们再次执行,就无法正确分割数据了
我们修改下代码,加上delimiter就行了,详情参考官网:https://docs.python.org/3/library/csv.html#csv-fmt-params
|
|
这时候,如果我们的数据中,含有分隔符,我们需要再加上封闭符,一般都会使用双引号,这里使用参数quotechar指定,默认是双引号
csv.writer(csvfile, dialect=’excel’, **fmtparams)Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method.
这里的用法都差不多,我们简单举个小例子,用官网的例子
这里用到了另一个参数:quoting,这个参数是针对quotechar来说的,
quotechar在ETL工具中叫做封闭符,是为了防止字段内容中出现分割符,我们需要区分到底是分隔符,还是字段内容,所以需要根据quotechar去判断;
quoting则是控制在什么情况下使用封闭符,他有几个选项
好了,csv的就简单分享到这里了。
昨天用到了这个collections模块,挺好用的,这里记录下。
官网介绍:https://docs.python.org/3/library/collections.html
博客:廖雪峰的博客
这里介绍些好玩儿的例子。
collections.namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)
Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful repr() method which lists the tuple contents in a name=value format.
namedtuple是一个工厂函数,返回一个自定义的tuple类,可读性更强些。
通常我们使用tuple的时候,像这样
我们是那个namedtuple就可以这样了
这样使用一个坐标位置,是不是可读性更强呢,而且用起来也很方便
我们可以看看这个Point是怎样定义的
下面还有个更好用的地方,我们再读取CSV或者数据库的时候,会返回结果集,这个时候用起来更方便,比如:
_make
我们使用list的时候,用下标查找很快,数据量大的时候,插入删除比较慢,deque是为了高效实现插入和删除的双向队列。
deque:double-ended queue
class collections.deque([iterable[, maxlen]])
Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty.
|
|
这里扩展了很多方便的函数,appendleft(),popleft()等等
可以设置默认值的dict,平时我们使用dict的时候,如果key不存在,会报错
class collections.defaultdict([default_factory[, …]])
Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.
|
|
我们使用defaultdict就可以避免这个错误
这里我们设置默认是int型,默认值为0
是一个简单的计数器,
class collections.Counter([iterable-or-mapping])
A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.
|
|
这个most_common最好用了感觉,根据次数进行排名
当然,collections中还有很多其他的好用的类,我们可以参考官方文档。
以前搞过Cognos,写过很多基础的教程,应该是14年的样子,都在CSDN上,这里贴个汇总贴吧,想要看的同学可以去看看,希望有帮助。
ReportStudio入门教程:http://blog.csdn.net/column/details/ygy-reportstudio.html
Framework Manage入门教程:http://blog.csdn.net/column/details/ygy-frameworkmanager.html
Cognos函数手册:http://blog.csdn.net/column/details/ygy-cognos-function.html
Cognos相关的其他资料(主页不同的类别下看看):http://blog.csdn.net/yuguiyang1990
好了,感兴趣的同学,可以自行去看看,好久不搞了,估计有疑问也解决不了了…