Python determine a blank column in a csv

AlexW

I'm making a config generating template in python, and it all works this far apart from my if row == '' section.

So I'm trying to check if a csv has blank data for the column BT and, if it's blank, remove any line that has the variable $BTSUB in it.

It's not working currently and I'm presuming it's because a csv empty column is not seen as '', does anyone know what it is seen as?

Also does anyone see any ways to improve what I have done below? (it's my first script)

Thanks Alex

import csv
import os
with open('Data/ShowroomData.csv', 'rt') as Data:
    SR = csv.DictReader(Data, delimiter=',', quotechar='|')
    for row in SR:
        # if you were to print just `row`, you would get a dictionary

        # set config file path
        folder  = 'Data/'
        filename = row['Host']
        path = folder + '/' + filename
        file = path + '/STR-' + filename + '-RTR-01.txt'
        # check if path exists if not make path
        if not os.path.exists(path):
            os.makedirs (path)
        # Read in the config file
        R1 = None
        with open('Data/STR-RTR-01.txt', 'r') as R1Template, open(file, 'w') as configfile:
            R1 = R1Template.read()

            # Replace the target string
            R1 = R1.replace('$STR', row['Host'])
            R1 = R1.replace('$IP', row['Subnet'])
            R1 = R1.replace('$DMVPN-DSL', row['DMVPN-DSL'])
            R1 = R1.replace('$DMVPN-4G', row['DMVPN-4G'])
            R1 = R1.replace('$BGPASNO', row['BGPAS'])
            R1 = R1.replace('$KCIP', row['KCIP'])
            R1 = R1.replace('$WRIP', row['WRIP'])
            R1 = R1.replace('$LOIP', row['Loopback'])              
            R1 = R1.replace('$DSL-USER', row['DSL-USER'])
            R1 = R1.replace('$DSL-PASS', row['DSL-PASS'])
            R1 = R1.replace('$Date', row['InstallDate'])

            if row['BT'] == (''):
                for line in R1:
                    if '$BTSUB' not in line:
                        # Write file without Static Routes
                        configfile.write(line)
            else:
                R1 = R1.replace('$BTSUB', row['BT'])
                # Write the file 
                configfile.write(R1)

EDIT: debug output

{'DMVPN-DSL': '79', 'DSL-PASS': '', 
 'Host': 'TEST', 'DSL-USER': '',
 'InstallDate': '', 'BGPAS': 'XXX', 'BT': '', 
 'Loopback': '172.X.X.X', 'KCIP': '172.XX.X.X', 
 'WRIP': '172.X.X.X', 'Location': 'Test', 
 'DMVPN-4G': '162', 'Subnet': '137'
}
Maresh

So the problem does not come from if row['BT'] == (''): but from your loop: for line in R1: When you do this:

R1 = R1Template.read()

R1 becomes a string. And when you loop on a string you get:

In [69]: for line in "abcd":
   ....:     print(line)
a
b
c
d

Therefore this condition will never be true (because line is just one character):

if '$BTSUB' not in line:

There're several way to fix this, the quickest, given your code structure is to change your for statement:

for line in R1.split('\n'): # If it's unix-like line endings

Although it's probably not the most efficient way.

If you need to keep the line return:

configfile.write("{}\n".format(line))

Or use a list and writelines()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related