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 相关文章推荐
取numpy数组的某几行某几列方法
Apr 03 Python
python实现微信自动回复功能
Apr 11 Python
使用Python AIML搭建聊天机器人的方法示例
Jul 09 Python
python 切换root 执行命令的方法
Jan 19 Python
Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析
Apr 27 Python
Python两台电脑实现TCP通信的方法示例
May 06 Python
用Python配平化学方程式的方法
Jul 20 Python
在tensorflow中设置使用某一块GPU、多GPU、CPU的操作
Feb 07 Python
python和c语言哪个更适合初学者
Jun 22 Python
pytorch 常用函数 max ,eq说明
Jun 28 Python
Django给表单添加honeypot验证增加安全性
May 06 Python
使用qt quick-ListView仿微信好友列表和聊天列表的示例代码
Jun 13 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
解析将多维数组转换为支持curl提交的一维数组格式
2013/07/08 PHP
PHP设计模式之工厂模式定义与用法详解
2018/04/03 PHP
使用PHP访问RabbitMQ消息队列的方法示例
2018/06/06 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
php让json_encode不自动转义斜杠“/”的方法
2020/04/27 PHP
JQuery1.4+ Ajax IE8 内存泄漏问题
2010/10/15 Javascript
JavaScript 5 新增 Array 方法实现介绍
2012/02/06 Javascript
Js数组的操作push,pop,shift,unshift等方法详细介绍
2012/12/28 Javascript
Bootstrap每天必学之js插件
2015/11/30 Javascript
轻松使用jQuery双向select控件Bootstrap Dual Listbox
2015/12/13 Javascript
JS根据浏览器窗口大小实时动态改变网页文字大小的方法
2016/02/25 Javascript
jQuery中Datatables增加跳转到指定页功能
2017/02/08 Javascript
Node.js发送HTTP客户端请求并显示响应结果的方法示例
2017/04/12 Javascript
vue.js移动数组位置,同时更新视图的方法
2018/03/08 Javascript
微信小程序实现顶部导航特效
2019/01/28 Javascript
nuxt.js写项目时增加错误提示页面操作
2020/11/05 Javascript
JavaScript十大取整方法实例教程
2020/12/03 Javascript
[01:53]3.19 DOTA2发布会 现场精彩Coser表演
2014/03/25 DOTA
[01:36:17]DOTA2-DPC中国联赛 正赛 Ehome vs iG BO3 第一场 1月31日
2021/03/11 DOTA
Python3 Random模块代码详解
2017/12/04 Python
Python中的defaultdict与__missing__()使用介绍
2018/02/03 Python
Python使用一行代码获取上个月是几月
2018/08/30 Python
python读取txt文件,去掉空格计算每行长度的方法
2018/12/20 Python
python 同时运行多个程序的实例
2019/01/07 Python
tensorflow如何批量读取图片
2019/08/29 Python
详解Python中打乱列表顺序random.shuffle()的使用方法
2019/11/11 Python
Python3 实现爬取网站下所有URL方式
2020/01/16 Python
Python代码中如何读取键盘录入的值
2020/05/27 Python
Python连接mysql数据库及简单增删改查操作示例代码
2020/08/03 Python
幼儿园大班毕业感言
2014/02/06 职场文书
初中军训感想300字
2014/03/05 职场文书
幼儿园六一儿童节文艺汇演主持词
2014/03/21 职场文书
葬礼司仪主持词
2014/03/31 职场文书
我的1919观后感
2015/06/03 职场文书
Python万能模板案例之matplotlib绘制直方图的基本配置
2022/04/13 Python
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技