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 相关文章推荐
python jieba分词并统计词频后输出结果到Excel和txt文档方法
Feb 11 Python
Django项目中用JS实现加载子页面并传值的方法
May 28 Python
使用python获取电脑的磁盘信息方法
Nov 01 Python
Pytorch 抽取vgg各层并进行定制化处理的方法
Aug 20 Python
python 使用pdfminer3k 读取PDF文档的例子
Aug 27 Python
Python读取配置文件(config.ini)以及写入配置文件
Apr 08 Python
Python基于codecs模块实现文件读写案例解析
May 11 Python
PyQt5 文本输入框自动补全QLineEdit的实现示例
May 13 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
Django URL参数Template反向解析
Nov 24 Python
python用700行代码实现http客户端
Jan 14 Python
聊聊基于pytorch实现Resnet对本地数据集的训练问题
Mar 25 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
在命令行下运行PHP脚本[带参数]的方法
2010/01/22 PHP
php实现将二维关联数组转换成字符串的方法详解
2017/07/31 PHP
PHP使用标准库spl实现的观察者模式示例
2018/08/04 PHP
将文本输入框内容加入表中的js代码
2013/08/18 Javascript
通过JS动态创建一个html DOM元素并显示
2014/10/15 Javascript
可输入文字查找ajax下拉框控件 ComBox的实现方法
2016/10/25 Javascript
JavaScript中数组Array方法详解
2017/02/27 Javascript
微信小程序canvas写字板效果及实例
2017/06/15 Javascript
node跨域请求方法小结
2017/08/25 Javascript
利用SpringMVC过滤器解决vue跨域请求的问题
2018/02/10 Javascript
详解如何使用babel进行es6文件的编译
2018/05/29 Javascript
layer.js open 隐藏滚动条的例子
2019/09/05 Javascript
Vue3.0 响应式系统源码逐行分析讲解
2019/10/14 Javascript
微信小程序wx.navigateTo方法里的events参数使用详情及场景
2020/01/07 Javascript
es6中new.target的作用和使用场景简单示例分析
2020/03/14 Javascript
用js实现放大镜效果
2020/10/28 Javascript
Python查询阿里巴巴关键字排名的方法
2015/07/08 Python
Python的Tornado框架实现异步非阻塞访问数据库的示例
2016/06/30 Python
spark: RDD与DataFrame之间的相互转换方法
2018/06/07 Python
Django使用中间件解决前后端同源策略问题
2019/09/02 Python
python3 xpath和requests应用详解
2020/03/06 Python
python实现同一局域网下传输图片
2020/03/20 Python
Python基于xlrd模块处理合并单元格
2020/07/28 Python
Python常用base64 md5 aes des crc32加密解密方法汇总
2020/11/06 Python
HTML5中的Web Notification桌面右下角通知功能的实现
2018/04/19 HTML / CSS
2013年员工自我评价范文
2013/12/27 职场文书
物理力学求职信
2014/02/18 职场文书
成龙洗发水广告词
2014/03/14 职场文书
农村婚庆司仪主持词
2014/03/15 职场文书
事假请假条范文
2014/04/11 职场文书
村党支部换届选举方案
2014/05/02 职场文书
万能检讨书开头与结尾怎么写
2015/02/17 职场文书
2015年计生工作总结范文
2015/04/24 职场文书
员工工作心得体会
2019/05/07 职场文书
解决Golang中ResponseWriter的一个坑
2021/04/27 Golang
MySQL对数据表已有表进行分区表的实现
2021/11/01 MySQL