【问题标题】:Script fails to read file from beginning脚本无法从头读取文件
【发布时间】:2020-06-23 19:15:39
【问题描述】:

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

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

EDIT3

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

【问题讨论】:

  • 您能提供输入文件的样本吗?也请写下你得到的输出。
  • 这样做的目的是什么?想要的输出是什么?为什么不使用 XML 库,例如 ElementTree?
  • @mzjn 我只想在开头阅读整个文件,然后做一些事情。我认为txt会更容易。我以前从未使用过 xml 文件。
  • “做一些事情”是什么意思?
  • @mzjn 例如,某些行以

标签: python python-3.x xml


【解决方案1】:

我想先纠正一点。

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

【讨论】:

  • 还是一样的:/
  • 你能显示输入文件的内容和你收到的输出吗?
【解决方案2】:

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

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

【讨论】:

  • 它仍然给我相同的输出:/
  • 真的吗?然后给出一些输入数据,以便我们可以重现......
  • 您是指正在搜索的文件吗?我用这个更新了答案。我也在脚本开始打印的位置添加了注释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多