擅长:python、mysql、java
<p>我觉得你很接近。您需要将字段名传递给<code>writer</code>,然后可以直接编辑<code>row</code>,因为它只是一个字典。例如:</p>
<pre><code>with open(inputFileName, "rb") as infile, open(outputFileName, "wb") as outfile:
r = csv.DictReader(infile)
w = csv.DictWriter(outfile, r.fieldnames)
w.writeheader()
for row in r:
if not row["Author"].strip():
row["Author"] = "DMD"
w.writerow(row)
</code></pre>
<p>转弯</p>
<pre><code>a,b,c,d,e,Author,g,h
1,2,3,4,5,Smith,6,7
8,9,10,11,12,Jones,13,14
13,14,15,16,17,,18,19
</code></pre>
<p>进入</p>
<pre><code>a,b,c,d,e,Author,g,h
1,2,3,4,5,Smith,6,7
8,9,10,11,12,Jones,13,14
13,14,15,16,17,DMD,18,19
</code></pre>
<p>我喜欢使用<code>if not somestring.strip():</code>,因为这样就无需考虑是否有空格、一个空格、十七个空格和一个制表符。我也更喜欢<code>DictReader</code>而不是标准阅读器,因为这样你就不必记住<code>Author</code>所在的列。</p>
<p>[注:以上假设Python 2,而不是3。]</p>