擅长:python、mysql、java
<p>您的问题并不完全清楚,因为您没有显示输出样本,但这里有一个有根据的猜测:</p>
<pre><code>count_cusip = 0
count_name = 0
count_sender = 0
cur_sender = ''
for line in lines:
m = re.search('[0-9]{3}[a-zA-Z0-9]{6}', line)
if m:
count_cusip += 1
ws.write(count_cusip,0,m.group(0))
ws.write(count_cusip,2,cur_sender)
n = re.search('[A-Z]{3,5}\s[0-9]{1,4}\D{1,3}\S{1,3}\s{1,2}\w+', line)
if n:
count_name += 1
ws.write(count_name,1,n.group(0))
ws.write(count_name,2,cur_sender)
o = re.search(r"Original Sender:\s\b\w+\s\w+", line)
if o:
count_sender += 1
cur_sender = o.group(0)
</code></pre>
<p>您不需要在遇到原始发送方时写入它,而是需要保存它并为每个cusip和名称写入当前值。</p>