Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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_Bash_Text_Sed_Ed - Fatal编程技术网

Python 以编程方式交换两块文本

Python 以编程方式交换两块文本,python,bash,text,sed,ed,Python,Bash,Text,Sed,Ed,我有一个由多个非常相似的块组成的XML文件。这里有两个: <Grid Name="EMFieldMany" GridType="Uniform"> <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/> <Attribute AttributeType="Scalar" Name="Er" Center="Node"> <Da

我有一个由多个非常相似的块组成的XML文件。这里有两个:

    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-0
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>
    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-1
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>


使用
sed
匹配文本块,但我不确定以后如何操作选定的块。这会上下交换单行,但我不确定如何将其推广到文本块,并使其交换块。

如果您将XML作为字符串,则可以使用replace来实现这一点:

#Swapping ( '\"' the slash escape the " to make it the string caracter)
XML_str = XML_str.replace("\"r","SomethingThatNeverAppearInTheXML")
XML_str = XML_str.replace("\"z","\r")
XML_str = XML_str.replace("SomethingThatNeverAppearInTheXML","\"z")
#Replacing
XML_str = XML_str.replace("x y z","z y x")
输入

XML_str = """    
    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-0
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>
    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-1
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>
"""
XML_str=”“”
字段\u时间\u 3D.hdf5:/Field/Er-0
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
字段\u时间\u 3D.hdf5:/Field/Er-1
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
"""
输出

<Grid Name="EMFieldMany" GridType="Uniform">
    <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
    <Attribute AttributeType="Scalar" Name="Er" Center="Node">
        <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/field/Er-0
        </DataItem>
    </Attribute>
    <Geometry GeometryType="VXVYVZ">
        <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/r
        </DataItem>
        <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/theta
        </DataItem>
        <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/z
        </DataItem>
    </Geometry>
</Grid>
<Grid Name="EMFieldMany" GridType="Uniform">
    <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
    <Attribute AttributeType="Scalar" Name="Er" Center="Node">
        <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/field/Er-1
        </DataItem>
    </Attribute>
    <Geometry GeometryType="VXVYVZ">
        <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/r
        </DataItem>
        <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/theta
        </DataItem>
        <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/z
        </DataItem>
    </Geometry>
</Grid>

字段\u时间\u 3D.hdf5:/Field/Er-0
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
字段\u时间\u 3D.hdf5:/Field/Er-1
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z

如果您将XML作为字符串,则可以使用replace执行此操作:

#Swapping ( '\"' the slash escape the " to make it the string caracter)
XML_str = XML_str.replace("\"r","SomethingThatNeverAppearInTheXML")
XML_str = XML_str.replace("\"z","\r")
XML_str = XML_str.replace("SomethingThatNeverAppearInTheXML","\"z")
#Replacing
XML_str = XML_str.replace("x y z","z y x")
输入

XML_str = """    
    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-0
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>
    <Grid Name="EMFieldMany" GridType="Uniform">
        <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
        <Attribute AttributeType="Scalar" Name="Er" Center="Node">
            <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/field/Er-1
            </DataItem>
        </Attribute>
        <Geometry GeometryType="VXVYVZ">
            <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/r
            </DataItem>
            <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/theta
            </DataItem>
            <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
                Field_reflected_time_3D.hdf5:/coordinates/z
            </DataItem>
        </Geometry>
    </Grid>
"""
XML_str=”“”
字段\u时间\u 3D.hdf5:/Field/Er-0
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
字段\u时间\u 3D.hdf5:/Field/Er-1
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
"""
输出

<Grid Name="EMFieldMany" GridType="Uniform">
    <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
    <Attribute AttributeType="Scalar" Name="Er" Center="Node">
        <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/field/Er-0
        </DataItem>
    </Attribute>
    <Geometry GeometryType="VXVYVZ">
        <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/r
        </DataItem>
        <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/theta
        </DataItem>
        <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/z
        </DataItem>
    </Geometry>
</Grid>
<Grid Name="EMFieldMany" GridType="Uniform">
    <Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
    <Attribute AttributeType="Scalar" Name="Er" Center="Node">
        <DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/field/Er-1
        </DataItem>
    </Attribute>
    <Geometry GeometryType="VXVYVZ">
        <DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/r
        </DataItem>
        <DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/theta
        </DataItem>
        <DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
            Field_reflected_time_3D.hdf5:/coordinates/z
        </DataItem>
    </Geometry>
</Grid>

字段\u时间\u 3D.hdf5:/Field/Er-0
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z
字段\u时间\u 3D.hdf5:/Field/Er-1
字段\u时间\u 3D.hdf5:/coordinates/r
字段\u反射\u时间\u 3D.hdf5:/coordinates/theta
字段\u时间\u 3D.hdf5:/coordinates/z

正如您提到的sed,我建议使用perl解决方案,但是使用xml解析器解析xml更好

#!/usr/bin/perl

# changing input line separator
$/="</Grid>";

while ( $_=<> ) {
    s@(\s*<DataItem Name="r".*?</DataItem>)(\s*<DataItem Name="theta".*?</DataItem>)(\s*<DataItem Name="z".*?</DataItem>)@$3$2$1@s;
    s@<DataItem Dimensions="\K(\d+) (\d+) (\d+) @$3 $2 $1 @;
    print;
}
#/usr/bin/perl
#更改输入行分隔符
$/="";
while($\=){

s@(\s*正如您提到的sed,我建议使用这种perl解决方案,但是使用xml解析器解析xml更好

#!/usr/bin/perl

# changing input line separator
$/="</Grid>";

while ( $_=<> ) {
    s@(\s*<DataItem Name="r".*?</DataItem>)(\s*<DataItem Name="theta".*?</DataItem>)(\s*<DataItem Name="z".*?</DataItem>)@$3$2$1@s;
    s@<DataItem Dimensions="\K(\d+) (\d+) (\d+) @$3 $2 $1 @;
    print;
}
!/usr/bin/perl
#更改输入行分隔符
$/="";
while($\=){

s@(\s*这可以通过ed轻松完成:

g/<DataItem Name="r"/-ka\
/<DataItem Name="z"/\
-kb\
.,/\/DataItem/m'a\
+1,/\/DataItem/m'b

g/这可以通过ed轻松完成:

g/<DataItem Name="r"/-ka\
/<DataItem Name="z"/\
-kb\
.,/\/DataItem/m'a\
+1,/\/DataItem/m'b

g/你能发布你到目前为止尝试过的内容吗?当然。我更新了我的问题。我不确定如何使用
sed
操作文本块,或者是否可以轻松操作。使用适当的面向XML的工具。不要尝试使用字符串替换,它迟早会咬到你。你能发布你到目前为止尝试过的内容吗?当然。我更新了我的问题。我不知道如何使用
sed
操作文本块,或者是否可以轻松操作。使用适当的面向XML的工具。不要尝试使用字符串替换,它迟早会咬到你的。这是什么语言或shell?是python,我对模式识别有一些问题,但在这方面做得很好。哎呀,我想我知道了我不清楚。更改
的名称将不起作用,因为通常
r
z
将具有不同的维度。我真的想交换整个
块。也许使用一些正则表达式可以做一些事情,但我认为最好使用一些我不熟悉的XLM库,抱歉。这里有一个教程,你想了解一下:。祝你好运!这是什么语言或shell?它是python,我对模式重新定义有一些问题,但在那里进行了修改。哎呀,我想我还不清楚。更改
的名称将不起作用,因为通常
r
z
将具有不同的di尺寸。我真的很想交换整个
块。也许用一些正则表达式你可以做些什么,但我认为最好使用一些我不熟悉的XLM库,对不起。这里有一个教程,你想了解一下:。祝你好运!这个语法太简单了!我想我必须学习一些Perl!这个语法太简单了!我想我想我得学点Perl!