<p>基本上,这就是我要做的。我有一个用python3读入的CSV文件。下面是行的基本布局:</p>
<pre><code>Row1: On
Row2: <empty>
Row3: <empty>
Row4: On
Row5: On
Row6: <empty>
Row7: Off
Row8: <empty>
</code></pre>
<p>访问的代码是:</p>
^{pr2}$
<p>运行脚本后,我希望看到每行的输出是:</p>
<pre><code>for row in file:
print(var)
On
On
On
On
On
On
Off
Off
</code></pre>
<p>我不知道该怎么做,但我试图在程序通过for循环时跟踪变量。逻辑如下:</p>
<pre><code>for loop:
1. If row[0] has the string 'On', then assign 'On' to var
2. If the next row[0] is empty, then I want the var to retain the previous value of 'On'.
3. Var will not change from 'On' until row[0] has a different value such as 'Off'. Then, Var will be assigned 'Off'.
</code></pre>
<p>希望这个问题有意义。我不知道如何在Python3中实现这一点。在</p>