【问题标题】:Python 3.x Reading files with headers (data between identified headers)Python 3.x 读取带有标题的文件(已识别标题之间的数据)
【发布时间】:2016-05-29 02:36:04
【问题描述】:

我使用的是 Python 3.4,并且已经安装了 NunPy/SciPy。 我需要读取具有以下结构的文本文件:

*Node
  1, -0.600000024,   1.20000005,           0.
  2, -0.600000024,  0.300000012,           0.
  3, -0.480560571,    0.1741862,           0.
  4, -0.335430175, 0.0791868418,           0.
  (...)
  n, x, y, z
*Element
  1, 1, 2, 3, 4
  2, 5, 6, 7, 8
  (...)
  n, a, b, c, d

从这个 txt 文件中,我需要创建一个名为“node”的矩阵,其中包含 *Node 和 *Element 之间的信息,我的意思是,它必须有 4 列和 n 行,例如:

node=array([1, -0.600000024, 1.20000005, 0.],[2, -0.600000024, 0.300000012, 0.], [3, -0.480560571, 0.1741862, 0.],[4, -0.335430175, 80.07 , 0.], [n, x, y, z])

还有一个叫做“元素”的矩阵,它包含 *Element: 之后的行

元素=数组([1, 1, 2, 3, 4], [2, 5, 6, 7, 8], [n, a, b, c, d])

确实,我只需要“读取”文本文件并将此内容写入两个矩阵。但是,我必须将 *Node 下的信息与 *Element 下的信息分开。我必须有两个矩阵,一个带有节点,另一个带有元素...但是我是 Python 新手,不知道如何以这种方式读取文本文件并生成这些矩阵...

如果有任何帮助/示例,我将不胜感激。非常感谢!

【问题讨论】:

  • 是整个文件吗? (在节点/元素数据之前或之后都没有?)通常在 abaqus 输入文件中有更多关键字,但如果仅此而已,它当然会简化事情。
  • 是的,这就是整个文件...我的意思是,我必须为大学的一门课程编写自己的有限元求解器 (2D)。而且由于我是 Abaqus 用户,我将读取 Abaqus 格式的节点和元素,因为这样做,我可以以更简单的方式准备模型(使用一些预处理器)。我怀疑是否应该使用 Python 或 Matlab/Octave。我对两者都只有非常基本的知识,所以无论如何我都必须学习,然后我选择了 Python。这是更好的选择吗? =D

标签: python numpy matrix text abaqus


【解决方案1】:

使用文件中的行创建一个列表,然后创建从 index 开始和停止的子列表,如果 '*Node''*Element' 应该适合您:

r=[]
s = open('File.txt')
For line in s:
  r.append(line.strip('\n'))
Node=[]
Element=[]
For i in r[r.index('*Node')+1:r.index('*Element')]:
  Node.append(map(float,i.split(',')))
For j in r[r.index('*Element')+1:]:
  Element.append(map(int, j.split(','))
Node=np.array(Node)
Element=np.array(Element)

【讨论】:

  • 您好 EoinS,感谢您的回答。我不明白的用法: Node.append(map(float,i.split(','))) 为什么不只使用: Node.append(i.split(',')) 如果我使用“ map",我如何检索要对它们进行操作的值?谢谢! Obs.:对不起,我刚开始使用 Python...
  • @Paulo 对于 map,第一个参数是一个函数,因此您可以通过定义一个处理一个值并将其作为第一个参数传入的函数来对这些值进行操作。这里float用于将split给出的字符串转换为浮点数,而不是字符串。
【解决方案2】:

@EoinS 有一个很好的解决方案,我想提出一个替代方案,它在确定两个列表类型的开始和结束位置方面不太动态,但将处理 CSV 格式中的各种边缘情况,并且列名是以下:

import numpy as np

node_rows = 75 #number of lines in the node section
interstitial_rows = 5 #number of lines between the last node record and the first element
element_rows = 1000 #the number of element rows

nodes = np.genfromtxt(
    'sample.csv',           # file name
    skip_header=1,          # lines to skip at the top, i assume there is a header
    skip_footer=(interstitial_rows + 1 + element_rows),          # lines to skip at the bottom, add one for the column name row at the end
    delimiter=',',          # column delimiter
    names=['n', 'x', 'y', 'z'])

elements = np.genfromtxt(
    'sample.csv',           # file name
    skip_header=(1 + node_rows + interstitial_rows),          # lines to skip at the top, i assume there is a header
    skip_footer=(1),          # one for the column name row at the end
    delimiter=',',          # column delimiter
    names=['n', 'a', 'b', 'c', 'd'])

请注意,我没有测试过这段代码,我一直使用类似的代码,但可能存在语法错误或我遗漏的东西。

您可以找到有关如何使用 numpy herehere 读取 CSV 的更多信息

【讨论】:

  • 谢谢乔希!我试试看!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 2020-03-24
相关资源
最近更新 更多