当前位置: 首页 > news >正文

Python学习笔记202302

1、numpy.empty
作用:根据给定的维度和数值类型返回一个新的数组,其元素不进行初始化。
用法:numpy.empty(shape, dtype=float, order=‘C’)

2、logging.debug
作用:Python 的日志记录工具,这个模块为应用与库实现了灵活的事件日志系统的函数与类。
在这里插入图片描述
图片来源:
https://docs.python.org/zh-cn/3/library/logging.html
https://blog.csdn.net/weixin_41724044/article/details/81784974

3、assert函数
https://docs.python.org/3/reference/simple_stmts.html#assert
作用:Python assert(断言)用于判断一个表达式,在表达式条件为 false 的时候触发异常。断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况
用法:assert expressionassert expression [, arguments]
如果expression是True,那么什么反应都没有。但是如果expression是False,那么会报错AssertionError。

4、np.where函数
(1)np.where(condition):返回值是满足condition的元素的下标;
(2)np.where(condition, x, y):返回值是一个和condition的shape相同的numpy 数组,当满足条件condition时,返回值中的元素从x中取,否则从y中取。

5、python读取shapefile文件

import shapefile
sh=shapefile.Reader('./shp/shapefile_file.shp')
shapefile_records=sh.records()#records,according to arcgis fileds

6、python 中两个//表示:地板除,即先做除法(/),然后向下取整(floor)。

7、python netcdf: making a copy of all variables and attributes but one
解决方法:https://stackoverflow.com/questions/15141563/python-netcdf-making-a-copy-of-all-variables-and-attributes-but-one
代码:

import netCDF4 as nc
toexclude = ['ExcludeVar1', 'ExcludeVar2']with netCDF4.Dataset("in.nc") as src, netCDF4.Dataset("out.nc", "w") as dst:# copy global attributes all at once via dictionarydst.setncatts(src.__dict__)# copy dimensionsfor name, dimension in src.dimensions.items():dst.createDimension(name, (len(dimension) if not dimension.isunlimited() else None))# copy all file data except for the excludedfor name, variable in src.variables.items():if name not in toexclude:x = dst.createVariable(name, variable.datatype, variable.dimensions)dst[name][:] = src[name][:]# copy variable attributes all at once via dictionarydst[name].setncatts(src[name].__dict__)

或者(测试可用):

import netCDF4 as nc
import numpy as np
toexclude = ["TO_REMOVE"]
with nc.Dataset("orig.nc") as src, nc.Dataset("filtered.nc", "w") as dst:# copy attributesfor name in src.ncattrs():dst.setncattr(name, src.getncattr(name))# copy dimensionsfor name, dimension in src.dimensions.iteritems():dst.createDimension(name, (len(dimension) if not dimension.isunlimited else None))# copy all file data except for the excludedfor name, variable in src.variables.iteritems():if name not in toexclude:x = dst.createVariable(name, variable.datatype, variable.dimensions)dst.variables[name][:] = src.variables[name][:]

代码来源:https://stackoverflow.com/questions/15141563/python-netcdf-making-a-copy-of-all-variables-and-attributes-but-one
Python setattr() 函数:
功能:setattr() 函数对应函数 getattr(),用于设置属性值,该属性不一定是存在的。
用法:setattr(object, name, value)

8、dataframe and series
series 对象转字符串:df[['station']] = pd.Series(df['station'], dtype="string")
筛选满足条件的行:df_new = df.loc[df['station'].str.contains('姓名')]

Python将循环过程中产生的dataframe,按行合并,最终输出一个csv文件:

merge_result = []
for file in os.listdir(csv_path):df = pd.read_csv(os.path.join(csv_path,file),header = None,names=['field1','field2'])merge_result .append(df_new)
df_all = pd.concat(merge_result , axis=0)

9、dataframe输出csv文件,中文出现乱码问题
https://blog.csdn.net/chenpe32cp/article/details/82150074

df.to_csv("result.csv",encoding="utf_8_sig")
http://www.lryc.cn/news/22246.html

相关文章:

  • 2023年大数据面试开胃菜
  • 优雅的controller层设计
  • 同步、通信、死锁
  • 【聚类】谱聚类解读、代码示例
  • 最牛逼的垃圾回收期ZGC(1),简介
  • 微服务的Feign到底是什么
  • JavaScript 正则表达式
  • 【批处理脚本】-1.15-文件内字符串查找命令find
  • 【手撕面试题】JavaScript(高频知识点二)
  • Web学习1_HTML
  • 华为OD机试真题Java实现【靠谱的车】真题+解题思路+代码(20222023)
  • 【C++入门(下篇)】C++引用,内联函数,auto关键字的学习
  • 基于合作型Stackerlberg博弈的考虑差别定价和风险管理的微网运行策略研究(Matlab代码实现)
  • 2023年全国最新保安员精选真题及答案8
  • JavaScript高级程序设计读书分享之6章——MapSet
  • 改进的 A*算法的路径规划(路径规划+代码+毕业设计)
  • Tina_Linux存储性能参考指南
  • NCRE计算机等级考试Python真题(四)
  • LeetCode每周刷题总结2.20-2.26
  • u盘里删除的文件可以恢复吗?分享解决方法
  • 十、vben框架如何使用table来写报表
  • jQuery:入门
  • 实例3:树莓派呼吸灯
  • android适配ipv6,请求慢?
  • 【LeetCode】剑指 Offer(10)
  • 学校AI视频行为分析监测系统 opencv
  • 内存数据库的设计与实现(已在大型项目中应用)
  • Linux基础命令-stat显示文件的状态信息
  • SQL入门DEMO
  • 辉光管时钟学习制作及开源软硬件工程