脚本无法从开头读取文件

2024-09-27 09:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python脚本解析xml文件。问题是,当我打印这些行时,我可以看到脚本跳过了很多行,并且(几乎)从xml文件的中间开始。实际上,我将xml转换为txt,这样会更容易

这是我的密码:

file_fcm='path_to_file.txt'
with open(file_fcm,'r',encoding='utf-8') as hrd:
    hrd=hrd.readlines()
    for line in hrd:
        print(line)

编辑

下面是输入文件的示例。 我在脚本开始阅读的行中添加了一条注释:

<?xml version="1.0"?>
<database DSN="test" Database="test  " SMT_DB="&lt;SMT DB&gt;" UTCExportTime="2020-06-23 08:09:49" DD_version="2.1.5" ExportType="Partition">
    <framerates>
        <framerate Name="80" Description="Eighty Hertz" PseudoRate="Y">
        </framerate>
        <framerate Name="20" Description="Twenty Hertz" PseudoRate="Y">
        </framerate>
        <framerate Name="10" Description="Ten Hertz" PseudoRate="Y">
        </framerate>
        <framerate Name="5" Description="Five Hertz" PseudoRate="Y">
        </framerate>
        <framerate Name="1" Description="One Hertz" PseudoRate="Y">
        </framerate>
    </framerates>
    <unittypes>
        <unittype Name="ft/lb" Description="Foot Pounds">
        </unittype>
        <unittype Name="deg" Description="degrees">
        </unittype>
        <unittype Name="ft/sec" Description="feet per second">
        </unittype>
        <unittype Name="ft" Description="feet">
        </unittype>
        <unittype Name="ft/sec^2" Description="feet per second square">
        </unittype>
        <unittype Name="lb/ft^2" Description="pound per foot square">
        </unittype>
        <unittype Name="deg/sec" Description="degree per second">
        </unittype>
        <unittype Name="radians" Description="radians">
        </unittype>
        <unittype Name="mv" Description="millivolt">
        </unittype>
        <unittype Name="nm" Description="nautical miles">
        </unittype>
        <unittype Name="kts" Description="knots">
        </unittype>
        <unittype Name="inHg" Description="inches mercury">
        </unittype>
        <unittype Name="ft/min" Description="feet per minute">
        </unittype>
        <unittype Name="deg C" Description="degrees Celsius">
        </unittype>
        <unittype Name="mach" Description="mach">
        </unittype>
        <unittype Name="g" Description="gravity constant">
        </unittype>
        <unittype Name="MHz" Description="mega hertz">
        </unittype>
        <unittype Name="mbar" Description="milli bar">
        </unittype>
        <unittype Name="V" Description="volt">
        </unittype>
        <unittype Name="Vrms" Description="volt root mean square">
        </unittype>
        <unittype Name="rpm" Description="revolutions per minute">
        </unittype>
        <unittype Name="in/lb" Description="torque">
        </unittype>
        <unittype Name="amps" Description="amperes">
        </unittype>
        <unittype Name="degM" Description="degrees of motor">
        </unittype>
        <unittype Name="psi" Description="pounds per square inch">
        </unittype>
        <unittype Name="rad/sec" Description="radians per seconds">
        </unittype>
        <unittype Name="deg/deg" Description="degree per degree">
        </unittype>
        <unittype Name="ft/sec/kt" Description="feet per second per knot">
        </unittype>
        <unittype Name="ft/sec/mach" Description="feet per second per mach">
        </unittype>
        <unittype Name="unitless" Description="unitless">
        </unittype>
        <unittype Name="sec" Description="seconds">
        </unittype>
        <unittype Name="deg/(ft/sec)" Description="degrees per (feet per second)">
        </unittype>
        <unittype Name="deg/(ft/sec^2)" Description="degrees per (feet per seconds square)">
        </unittype>
        <unittype Name="%MAC" Description="Percent of Mach">
        </unittype>
        <unittype Name="deg/ft" Description="degrees per feet">
        </unittype>
        <unittype Name="sec/ft" Description="seconds per feet">
        </unittype>
        <unittype Name="lb" Description="Pounds">
        </unittype>
        <unittype Name="fc" Description="foot-candles">
        </unittype>
        <unittype Name="deg/sec^2" Description="degrees per second square">
        </unittype>
        <unittype Name="in" Description="inches">
        </unittype>
        <unittype Name="deg/(g*sec)" Description="degrees per (g times seconds)">
        </unittype>
        <unittype Name="mA" Description="mili amperes">
        </unittype>
    </unittypes>
    <datatypes>
        <datatype Name="uint32" Description="Unsigned Integer" BitSize="32" MinValue="0" MaxValue="4294967295" IntegerOnly="Y">
        </datatype>
        <datatype Name="int32" Description="Signed Integer" BitSize="32" MinValue="-2147483648" MaxValue="2147483647" IntegerOnly="Y">
        </datatype>
        <datatype Name="boolean" Description="Boolean" BitSize="32" MinValue="0" MaxValue="1" IntegerOnly="Y">
        </datatype>
        <datatype Name="float" Description="Single Precision Floating Point" BitSize="32" MinValue="-1073741824" MaxValue="1073741824" IntegerOnly="N">
        </datatype>
    </datatypes>
    <partitions>
        <partition Name="fcm" Description="Flight Control Module; DAL=A" SMT_PartitionID="000000000">
            <dataobjects>
                <Signal Name="test" Description="test- test point" Unit="deg" DataType="float" Scaling="" Sensing="Positive" InitialValue="0" MinRange="-50" MaxRange="50" IsTestPoint="Y" BitSize="">
                </Signal>
                <Signal Name="test" Description="test- test point" Unit="deg" DataType="float" Scaling="" Sensing="Positive" InitialValue="0" MinRange="-50" MaxRange="50" IsTestPoint="Y" BitSize="">
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="int32" Scaling="" Sensing="Positive" InitialValue="0" MinRange="-2147483648" MaxRange="2147483647" IsTestPoint="Y" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="int32" Scaling="" Sensing="Positive" InitialValue="0" MinRange="-2147483648" MaxRange="2147483647" IsTestPoint="Y" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                #THIS IS WHERE THE SCRIPT STARTS**<Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">** 
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="Y" BitSize="">
                </Signal>
                <Signal Name="test" Description="test" Unit="unitless" DataType="boolean" Scaling="" Sensing="Positive" InitialValue="0" MinRange="0" MaxRange="1" IsTestPoint="N" BitSize="">
                    <ProducedBy Function="test.test">
                    </ProducedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                    <ConsumedBy Function="test.test">
                    </ConsumedBy>
                </Signal>

EDIT2

我还有另外两个类似的xml文件,但是较小大小,脚本在它们上运行良好。这可能是问题吗?尺寸

EDIT3

是的,我将文件拆分为4个较小的文件,它可以正常工作。但为什么会发生这种情况?


Tags: nametestsignalunitfunctiondescriptiondatatypeper
2条回答

我想先纠正一件事

您将filepointer命名为hrd,然后再次使用相同的变量名存储readlines()的输出

试试这个:

file_fcm='path_to_file.txt'
with open(file_fcm,'r',encoding='utf-8') as hrd:
    file_content = hrd.readlines()

for line in file_content:
print(line)

您可以使用readlines来读取一堆行,或者枚举文件对象。如果将它们混合在一起,则readlines的输出中有一部分文件,并且只处理之后剩下的部分。读取文件的正确方法是:

file_fcm='path_to_file.txt'
with open(file_fcm,'r',encoding='utf-8') as hrd:
    for line in hrd:
        print(line)

相关问题 更多 >

    热门问题