Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python区分特定格式的文本_Python_Python 3.x_Python 2.7 - Fatal编程技术网

用Python区分特定格式的文本

用Python区分特定格式的文本,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,好吧,我有一个问题要提,我不确定该怎么表达,出于同样的原因,我在互联网上也找不到任何东西。但我有一个大的文本文件,它是Nastran作业的输出;以下是一个示例: D I S P L A C E M E N T V E C T O R POINT ID. TYPE T1 T2 T3 R1 R

好吧,我有一个问题要提,我不确定该怎么表达,出于同样的原因,我在互联网上也找不到任何东西。但我有一个大的文本文件,它是Nastran作业的输出;以下是一个示例:

                                         D I S P L A C E M E N T   V E C T O R

  POINT ID.   TYPE          T1             T2             T3             R1             R2             R3
    158819      G      1.709110E-04   7.605540E-16  -1.555717E-15  -4.860894E-18   1.829865E-03  -2.318361E-02
    158820      G      1.875926E-04   7.603908E-16  -1.382438E-15   2.883937E-18   3.121088E-04   2.332622E-02
    158821      G      1.275168E-04   6.281925E-16  -1.472595E-15  -1.425970E-17   4.282258E-03  -1.669750E-02
    158822      G      1.712281E-04   7.267596E-16  -1.473447E-15  -2.136693E-18   1.425850E-03   2.210620E-02
    158823      G      1.010464E-04   5.633097E-16  -1.426041E-15  -3.498301E-17   4.969753E-03  -1.144002E-02
    158824      G      1.274082E-04   6.164795E-16  -1.567410E-15  -5.947361E-18   3.224372E-03   1.580865E-02
                                                 L O A D   V E C T O R

  POINT ID.   TYPE          T1             T2             T3             R1             R2             R3
    158801      G      1.175810E+02  -3.610042E-16  -2.782717E-12   0.0            0.0            0.0
    158802      G      8.539756E+01   1.118419E-15  -2.639934E-12   0.0            0.0            0.0
    158803      G      9.717072E+01  -7.574143E-16  -2.682750E-12   0.0            0.0            0.0
    158804      G      1.175825E+02   0.0           -2.986422E-12   0.0            0.0            0.0
如您所见,Nastran通过在每个部分的标题中使用所有大写字母和每个字母之间的空格以及每个单词之间的三个空格来创建文件。所以我的问题是,如果我有一个脚本来查找点ID。在文件中,我如何编写它,以便如果我指定了一个节,它会查找带有格式的节名称,但也会在下一节停止,而不管该节的名称如何,但会根据节头的格式停止


非常感谢您的帮助,如果没有意义,请告诉我。

在我的头顶上,我将阅读文件,按sep=“”(三个空格)分割内容,最后创建一个单独部分的词典。标题是键,下面的行是值。

您可能一直在寻找的格式短语可以称为模式匹配,或

如果所有其他方法都失败了,您应该始终能够通过使用正则表达式找到一个部分的结尾来解决这个问题。 请注意,使用可以将数据导入pandas或类似的易于管理的逻辑来跟踪此代码可能仍然是明智的。 如果你想调整这个正则表达式,我觉得非常有用。希望这有帮助

import re    
#matches the pattern UPPERCASE,SPACE followed by (UPPERCASE, SPACE) repeated, 
#followed by TWO SPACE optional
#whole pattern can be repeated. 
#You may want to tweak the rules to make it more strict as necessary.

pattern = re.compile("(([A-Z] )([A-Z] )+(  )?)+")

test = '''                                         D I S P L A C E M E N T   V E C T O R

  POINT ID.   TYPE          T1             T2             T3             R1             R2             R3
    158819      G      1.709110E-04   7.605540E-16  -1.555717E-15  -4.860894E-18   1.829865E-03  -2.318361E-02
    158820      G      1.875926E-04   7.603908E-16  -1.382438E-15   2.883937E-18   3.121088E-04   2.332622E-02
    158821      G      1.275168E-04   6.281925E-16  -1.472595E-15  -1.425970E-17   4.282258E-03  -1.669750E-02
    158822      G      1.712281E-04   7.267596E-16  -1.473447E-15  -2.136693E-18   1.425850E-03   2.210620E-02
    158823      G      1.010464E-04   5.633097E-16  -1.426041E-15  -3.498301E-17   4.969753E-03  -1.144002E-02
    158824      G      1.274082E-04   6.164795E-16  -1.567410E-15  -5.947361E-18   3.224372E-03   1.580865E-02
                                                 L O A D   V E C T O R

  POINT ID.   TYPE          T1             T2             T3             R1             R2             R3
    158801      G      1.175810E+02  -3.610042E-16  -2.782717E-12   0.0            0.0            0.0
    158802      G      8.539756E+01   1.118419E-15  -2.639934E-12   0.0            0.0            0.0
    158803      G      9.717072E+01  -7.574143E-16  -2.682750E-12   0.0            0.0            0.0
    158804      G      1.175825E+02   0.0           -2.986422E-12   0.0            0.0            0.0'''

#your input for beginning of a section header    
begin = 'displacement vector'
#getting it into the correct style laid out here for section headers.
begin = ' '.join(begin.upper()) 
location = test.find(begin)

if location != -1:
    relevant_section = test[location + len(begin):]
else:
    relevant_section = test
#regex matching for end of section
end = pattern.search(relevant_section)
if end is not None:
    end = end.span()[0]
    relevant_section = relevant_section[:end]

我在想。你解决了你的问题吗?我解决了,很抱歉没有回答。效果很好。我会试试看,然后告诉你会发生什么。