python 工具 字符串转numpy浮点数组的实现


Posted in Python onMarch 14, 2020

不同的数字之间使用 空格“ ”,“$”,"*"等隔开,支持带小数点的字符串

NumArray=str2num(LineString,comment='#')

将字符串中的所有非Double类型的字符全部替换成空格

以'#'开头直至行尾的内容被清空

返回一维numpy.array数组

python 工具 字符串转numpy浮点数组的实现

import numpy
import scipy
def str2num(LineString,comment='#'):
 
  from io import StringIO as StringIO
  import re,numpy
 
  NumArray=numpy.empty([0],numpy.int16)
  NumStr=LineString.strip()
  #~ ignore comment string
  for cmt in comment:

    CmtRe=cmt+'.*$'
    NumStr=re.sub(CmtRe, " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
 
  #~ delete all non-number characters,replaced by blankspace.
  NumStr=re.sub('[^0-9.e+-]', " ", NumStr, count=0, flags=re.IGNORECASE)
 
  #~ Remove incorrect combining-characters for double type.
  NumStr=re.sub('[.e+-](?=\s)', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
  NumStr=re.sub('[.e+-](?=\s)', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
  NumStr=re.sub('[e+-]$', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
  NumStr=re.sub('[e+-]$', " ", NumStr.strip(), count=0, flags=re.IGNORECASE)
 
  if len(NumStr.strip())>0:
    StrIOds=StringIO(NumStr.strip())
    NumArray= numpy.genfromtxt(StrIOds)
 
  return NumArray


if __name__ == "__main__":
  str = input("Enter your input: ");
  donser=str2num(str)
  print(donser)

补充知识:Python 将numpy array由浮点型转换为整型

——使用numpy中的astype()方法可以实现,如:

python 工具 字符串转numpy浮点数组的实现

以上这篇python 工具 字符串转numpy浮点数组的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
flask中使用SQLAlchemy进行辅助开发的代码
Feb 10 Python
Python字符编码判断方法分析
Jul 01 Python
python爬虫之百度API调用方法
Jun 11 Python
Python3.6 Schedule模块定时任务(实例讲解)
Nov 09 Python
Python日期时间对象转换为字符串的实例
Jun 22 Python
Flask框架实现的前端RSA加密与后端Python解密功能详解
Aug 13 Python
PyQt5+Caffe+Opencv搭建人脸识别登录界面
Aug 28 Python
django框架中ajax的使用及避开CSRF 验证的方式详解
Dec 11 Python
浅谈python 中的 type(), dtype(), astype()的区别
Apr 09 Python
Python定义一个函数的方法
Jun 15 Python
Python基于yaml文件配置logging日志过程解析
Jun 23 Python
如何利用Python给自己的头像加一个小国旗(小月饼)
Oct 02 Python
python tkinter GUI绘制,以及点击更新显示图片代码
Mar 14 #Python
使用Python爬取弹出窗口信息的实例
Mar 14 #Python
解决django中form表单设置action后无法回到原页面的问题
Mar 13 #Python
浅谈在django中使用redirect重定向数据传输的问题
Mar 13 #Python
在Django中预防CSRF攻击的操作
Mar 13 #Python
Anaconda+Pycharm环境下的PyTorch配置方法
Mar 13 #Python
Pycharm中切换pytorch的环境和配置的教程详解
Mar 13 #Python
You might like
Windows 下的 PHP-PEAR 安装方法
2010/11/20 PHP
ThinkPHP中类的构造函数_construct()与_initialize()的区别详解
2017/03/13 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
2019/12/12 PHP
JSON语法五大要素图文介绍
2012/12/04 Javascript
javascript判断并获取注册表中可信任站点的方法
2015/06/01 Javascript
JavaScript中Math.SQRT2属性的使用详解
2015/06/14 Javascript
分享12个实用的jQuery代码片段
2016/03/09 Javascript
Js调用Java方法并互相传参的简单实例
2016/08/11 Javascript
Bootstrap精简教程中秋大放送
2016/09/15 Javascript
JavaScript实现星星等级评价功能
2017/03/22 Javascript
使用jQuery实现页面定时弹出广告效果
2017/08/24 jQuery
详解基于 axios 的 Vue 项目 http 请求优化
2017/09/04 Javascript
JS实现字符串去重及数组去重的方法示例
2018/04/21 Javascript
JS与SQL方式随机生成高强度密码示例
2018/12/29 Javascript
Python中map,reduce,filter和sorted函数的使用方法
2015/08/17 Python
python中执行shell的两种方法总结
2017/01/10 Python
Python使用jsonpath-rw模块处理Json对象操作示例
2018/07/31 Python
Python如何爬取实时变化的WebSocket数据的方法
2019/03/09 Python
python爬虫 模拟登录人人网过程解析
2019/07/31 Python
python-web根据元素属性进行定位的方法
2019/12/13 Python
python垃圾回收机制(GC)原理解析
2019/12/30 Python
关于Django Models CharField 参数说明
2020/03/31 Python
在keras中实现查看其训练loss值
2020/06/16 Python
浅谈CSS3中display属性的Flex布局的方法
2017/08/14 HTML / CSS
HTML5手机端弹出遮罩菜单特效代码
2016/01/27 HTML / CSS
C#基础面试题
2016/10/17 面试题
探亲邀请信范文
2014/01/30 职场文书
日本语毕业生自荐信
2014/02/01 职场文书
致100米运动员广播稿
2014/02/14 职场文书
导师评语大全
2014/04/26 职场文书
酒店采购员岗位职责
2015/04/03 职场文书
工厂门卫岗位职责
2015/04/13 职场文书
开展警示教育活动总结
2015/05/09 职场文书
法律意见书范本
2015/06/04 职场文书
孕妇病假条怎么写
2015/08/17 职场文书
html css3不拉伸图片显示效果
2021/06/07 HTML / CSS