如果没有值,python词典不会打印

时间:2017-12-14 14:15:02

标签: python dictionary

我写了一个简短的脚本,根据CSV文件中的信息打印一些信息。

我需要做的是,如果没有键的值,或者如果存在默认值,例如'n / a',则不打印打印功能。

或者,如果CSV中有默认或空单元格,它可能不会被添加到字典中?不确定什么是最好的选择。

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:')

CSV格式如下:

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

输出应该看起来像

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 

2 个答案:

答案 0 :(得分:0)

在给定条件的情况下,一种方法是在循环中打印键:

import csv

with open('lhcdes.csv', 'rb') as testcsv:
    myfile = csv.DictReader(testcsv)
    keys = ['key1', 'key2', 'key3']              # a list of all keys you want to print
    default_value = 'n/a'                        # the default value you want to skip
    for row in myfile:
        for key in keys:
            value = row.get(key, default_value)  # get the value at 'key', or the
                                                 # default value if 'key' was not found 
            if value == default_value:
                continue                         # skip to the next item in the 'keys' 
                                                 # list
            print("{} {}".format(key, value))    # use .format over '%' syntax

这样,您可以跳过任何没有值或默认值的条目。

答案 1 :(得分:0)

您需要针对Key5值测试Key6missing的内容,然后相应地格式化输出:

import csv

missing = ['Not Found', None, 'N/A', '']

with open('lhcdes.csv', 'rb') as testcsv:
    myfile = csv.DictReader(testcsv)

    for row in myfile:
        if row['Key5'] in missing or row['Key6'] in missing:
            print 'interface {}\nClient: {} {} IR:{}'.format(
                row['Key1'], row['Key2'], row['Key3'], row['Key4'])
        else:
            print 'interface {}\nClient: {} {} IR:{} CR:{} R:{}'.format(
                row['Key1'], row['Key2'], row['Key3'], row['Key4'], row['Key5'], row['Key6'])

该脚本将显示以下输出:

interface Gi0/3/0/1.1838
Client: CustA EU1 IR:AN-12345 CR:TAL12345 R:Host1_London
interface Gi0/3/0/1.2072
Client: CustB EU2 IR:AN-12346 CR:TAL12346 R:Host2_Manchester
interface Gi0/3/0/2.3761
Client: CustB EU3 IR:AN-12347
interface Gi0/3/0/3.3573
Client: CustC EU7 IR:AN-12348 CR:TAL12348 R:Host5_Swansea
interface Gi0/3/0/3.3702
Client: CustD EU5 IR:AN-12349
interface Gi0/3/0/3.3917
Client: CustB EU6 IR:AN-12350
interface Gi0/3/0/3.3918
Client: CustA EU2 IR:AN-12351
interface Gi0/3/0/3.3919
Client: CustE EU9 IR:AN-12352
interface Gi0/3/0/3.3923
Client: CustE EU9 IR:AN-12353
interface Gi0/3/0/4.512
Client: CustC EU8 IR:AN-12354