If I want to import a Nastran Deck into a Python Array, in one fell swoop using numpy. How can I go about it? Where am I going wrong?

I have a file with only Grids in it. Grids are all equally spaced with Nastran's Short translator (8 characters)

$ MH Nodes

$2345678$2345678$2345678$2345678$2345678$2345678

GRID 25601 58.50002-57.749923.05

GRID 25602 58.81002-57.749923.05

using the dtype command from Numpy, is great if I understand it correctly. Here is my code:

fileMH = "Gnodes.bdf"

dtyp = np.dtype([

("Grid",(np.void,8)),

("GN",(np.int,8)),

("Prop",(np.void,8)),

("X",(np.float,8)),

("Y",(np.float,8)),

("Z",(np.float,8)),

])

f = np.loadtxt(fileMH,dtyp,comments="$")

The error I get is a float error, however I was expecting dtype to extract 8 characters at a time out of the string. Here is the error:

ValueError: invalid literal for float(): 58.50002-57.749923.05

Help is appreciated.

P.S. Can some please make a nastran tag, huge amounts of data crunching is done in nastran, that requires smart programing. It would be helpful.

解决方案

The dtype specifies the layout of the binary data in memory. It doesn't define the format of text in a file.

Your data file has fixed-width fields. That is, each field uses eight characters, and there is no special delimiter character to separate the fields. You can read such a file using numpy.genfromtxt, by specifying the field widths in the delimiter argument.

Here's an example in an ipython session. First, here's your sample file. (I edited the file to ensure that there are three spaces after the final 05 in the second and third lines, but this wasn't necessary.)

In [15]: !cat nastran_data.txt

$ MH Nodes

$2345678$2345678$2345678$2345678$2345678$2345678

GRID 25601 58.50002-57.749923.05

GRID 25602 58.81002-57.749923.05

Here's the data type of the array to be created by genfromtxt. Note that the GN field has type np.int64, and the X, Y and Z fields have type np.float64.

In [16]: dt = np.dtype([('Grid', 'S8'), ('GN', np.int64), ('Prop', 'S8'), ('X', np.float64), ('Y', np.float64), ('Z', np.float64)])

Read the file using genfromtxt, with six fixed-width fields, each of length 8:

In [18]: a = np.genfromtxt('nastran_data.txt', dtype=dt, delimiter=(8, 8, 8, 8, 8, 8), skip_header=2)

In [19]: a

Out[19]:

array([('GRID ', 25601, ' ', 58.50002, -57.7499, 23.05),

('GRID ', 25602, ' ', 58.81002, -57.7499, 23.05)],

dtype=[('Grid', 'S8'), ('GN', '

In [20]: a['GN']

Out[20]: array([25601, 25602])

In [21]: a['X']

Out[21]: array([ 58.50002, 58.81002])

python调用nastran,使用numpy在Python中导入nastran节点卡片组相关推荐

  1. Extending Python with C (Python 调用 C) - A Simple Example - Python list

    Extending Python with C (Python 调用 C) - A Simple Example - Python list 原文阅读于极客学院 http://www.jikexuey ...

  2. python调用matlab函数_从 Python 调用 MATLAB 函数的三种方法

    0. 实验环境Ubuntu 16.04 Matlab R2015b 1. 借助于 mlab 库 安装方法非常简单,只需一行命令 sudo pip install mlab 即可. import num ...

  3. python 调用控制台_如何使用Python的交互控制台

    简介 Python的交互控制台(也叫做Python解释器,或是Python Shell)为程序员提供了"运行指令"和"不创建文件测试测试代码"的快速途径. 交互 ...

  4. Python调用海康威视网络相机之——python调用海康威视C++的SDK

    运行环境: Win10系统,64位,Anaconda3 python 3.5.5(基于anaconda环境) opencv 3.4.2 (早前就已经安装了,本文不会讲如何安装) Visual Stud ...

  5. python调用dll函数_从Python调用DLL函数

    我想从Python调用DLL中的函数.但我有个错误:"Attribute Error function not found" 这是我的代码:import os import cty ...

  6. python调用usb设备_用Python与USB设备通信

    假设您使用Linux和libusb-1.0作为PyUSB的后端库.// Detach a kernel driver from an interface. // If successful, you ...

  7. python调用excel宏_用Python如何开发Excel宏脚本?新手必学

    今天介绍一个叫 xlpython 的库,通过它我们可以用 Python 来开发 Excel 的宏脚本,真正实现在 Excel 中调用 Python. 基本环境 操作系统:Windows 10 x64 ...

  8. 在windows中python安装sit-packages路径位置 在Pycharm中导入opencv不能自动代码补全问题

    一.在windows中python安装sit-packages路径位置 C:\Users\shl\AppData\Local\Programs\Python\Python36\Lib\site-pac ...

  9. python 调用 .netcore api_浅谈Python调用XBee的API来进行通讯

    浅谈Python调用XBee的API来进行通讯 用python编程来控制串口(COM口),来让一对XBee进行通讯.不需要借助终端来发送和接收数据,增大了XBee使用的灵活性.这才是使用XBee模块的 ...

最新文章

  1. named 客户端无法解析_解析天联高级版客户端无法访问用友U8问题
  2. 2008找回企业久违的网速
  3. GAN处理手写图片数据集
  4. C#——实现IComparableT 接口,ArrayLIst调用ArrayLIst.Sort()抛出System.InvalidOperationException异常解决方案
  5. linux 恢复boot分区,Linux boot分区格式化后恢复 示例
  6. springboot中来进行文件下载
  7. 谈谈Python和其他语言的区别
  8. Magisk —— 安卓新一代的第三方拓展,systemless模式
  9. leetcode练习旋转数组
  10. mongodb 扩展 libmongoc ssl_扩展面板
  11. 前端笔记之——ajax请求出现406的原因
  12. 题目:js实现求100以内的质数
  13. 怎样允许计算机远程桌面连接,win10允许远程桌面连接如何设置_win10怎样设置允许远程桌面连接...
  14. 输入水仙花数的位数 c语言,水仙花数,水仙花数c语言
  15. ios实时卡顿检测和优化方案
  16. Django中多对多查询方式
  17. 自下而上和自上而下的注意力模型《Bottom-Up and Top-Down Attention for Image Captioning and Visual Question Answering》
  18. Fedora16+XAMPP+Drupal7建站,从零开始全程指导
  19. 十二时辰手把手教你入门Spring
  20. 300万+企业财税服务平台微企宝,将于8月8日全球首发QB生态通证

热门文章

  1. spams工具箱在MATLAB下的安装编译
  2. 微机原理与接口技术模拟试题微型计算机中主要包括,合肥工业大学微机原理与接口技术模拟试题...
  3. 学习地图配色:以制作省行政区划图+常住人口密度分布图为例
  4. Https Http2 Spdy 学习笔记
  5. 软件开发简历应该怎么写?
  6. 详讲FL Studio低频振荡器命令
  7. pytorch ssim计算
  8. micropython开发idethonny_用ESP32+MicroPython+Thonny点亮WS2812灯带
  9. 广东印发知识产权人才“十四五”规划
  10. 爬去豆瓣网中电影信息并保存到本地目录当中