回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我已经写了一个简短的脚本打印一些信息的基础上的信息在一个CSV文件。你知道吗</p>
<p>我需要能够做的是,使打印功能不打印,如果没有值的关键,或如果有一个默认值,如'n/a'。你知道吗</p>
<p>或者,如果CSV中有一个默认的或空的单元格,它就不会被添加到字典中?不知道什么是最好的选择。你知道吗</p>
<pre><code>import csv
with open('lhcdes.csv', 'rb') as testcsv:
myfile = csv.DictReader(testcsv)
for row in myfile:
print 'Key1 %s' % row.get('Key1') + '\n' + 'and ' + 'Key2:%s ' % row.get('Key2') + 'Key3:%s ' % row.get('Key3:')
</code></pre>
<p>CSV格式如下:</p>
<pre><code>Key1,Key2,Key3,Key4,Key5,Key6
Gi0/3/0/1.1838,CustA,EU1,AN-12345,TAL12345,Host1_London
Gi0/3/0/1.2072,CustB,EU2,AN-12346,TAL12346,Host2_Manchester
Gi0/3/0/2.3761,CustB,EU3,AN-12347,TAL12347,Not Found
Gi0/3/0/3.3573,CustC,EU7,AN-12348,TAL12348,Host5_Swansea
Gi0/3/0/3.3702,CustD,EU5,AN-12349,N/A,Host4_Glasgow
Gi0/3/0/3.3917,CustB,EU6,AN-12350,TAL12350,Not Found
Gi0/3/0/3.3918,CustA,EU2,AN-12351,TAL12351,N/A
Gi0/3/0/3.3919,CustE,EU9,AN-12352,Not Found,Not Found
Gi0/3/0/3.3923,CustE,EU9,AN-12353,TAL12353,N/A
Gi0/3/0/4.512,CustC,EU8,AN-12354,TAL12354,Not Found
</code></pre>
<p>输出应该如下所示</p>
<pre><code>interface Gi0/3/0/1.1838
Client:CustA EU:EU1 IR:AN-12345 CR:TAL12345 R:Host1_London
interface Gi0/3/0/1.2072
Client:CustB EU:EU2 IR:AN-12346 CR:TAL12346 R:Host2_Manchester
Where info is absent or n/a
interface Gi0/3/0/3.3919
Client:CustE EU:EU9 IR:AN-12352
</code></pre>