脚本无法从头开始读取文件



我正在尝试使用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>

编辑2

我还有 2 个类似的 xml 文件,但尺寸较小,脚本在它们上工作正常。这可能是问题所在吗?大小?

编辑3

所以是的,我将文件分成 4 个较小的文件并且可以工作。为什么会这样呢?

我想先纠正一件事。

您已将文件指针命名为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)

最新更新