Python: Using CSV to parse(?) variables and then output that to another file -
i server administrator. i've skated without having scripting, alas -- has reared ugly head.
summary: have example.csv looks following;
stan,marsh,stan marsh,1001,899,smarsh,smarsh@info.com eric,cartman,eric cartman,1002,898,ecartman,ecartman@info.com
now. i'm trying read in csv file. then, want take value each row , put this;
dn: cn=$cn,ou=people,dc=domain,dc=com cn: $cn gidnumber: 20 givenname $fn homedirectory /home/users/$user loginshell: /bin/sh objectclass: inetorgperson objectclass: posixaccount objectclass: top sn: $ln uid: $username telephonenumber: $tele uidnumber: $uidn userpassword: {crypt}mrpoo mail: $email
as can see, i'm attempting make ldif file allows me import user names , auto fill in variables.
i can't seem put pieces together.
i haven't gotten far either. learned print rows, yay... !
import csv open('example.csv', 'rb') f: reader = csv.reader(f) row in reader: print row
i think logic follows.
- import .csv. loop through row.
- place data variables.
- output final product (print?) "output_file"
- loop until eof?
any appreciated.
something oughta work.
the csv module overkill file yours.
some of python idioms i'm using here:
dict(zip(keys, values))
-- zips list of keys , list of values;dict
function (ordict.update
) can digest key-value pairs add dict- mapping-form string interpolation (
%(foo)s
) can digest dict
the defaults
bit there string interpolation wouldn't choke on missing values. adapt needs. :)
.
if true: # testing -- use other branch read file # declare test content in string... input_content = """ stan,marsh,stan marsh,1001,899,smarsh,smarsh@info.com eric,cartman,eric cartman,1002,898,ecartman,ecartman@info.com """.strip() # , use stringio module create file-like object it. stringio import stringio input_file = stringio(input_content) else: # or open file normal. in short script this, # 1 doesn't need worry closing file - happen # when script ends. input_file = open('example.csv', 'rb') # declare fields in order in file. # zip() use later actual fields file # create dict mapping. fields = ('fn', 'ln', 'name', 'uidn', 'gidn', 'cn', 'email') # fields, in order # declare template ldif file. %(...)s bits # later interpolated dict mapping created each input row. template = u""" dn: cn=%(cn)s,ou=people,dc=domain,dc=com cn: %(cn)s gidnumber: 20 givenname %(fn)s homedirectory /home/users/%(user)s loginshell: /bin/sh objectclass: inetorgperson objectclass: posixaccount objectclass: top sn: %(ln)s uid: %(username)s telephonenumber: %(tele)s uidnumber: %(uidn)s userpassword: {crypt}mrpoo mail: %(email)s """ line in input_file: # create `vals` default values. these overwritten # if csv data (and of course declared fields) contain them. vals = {"user": "xxx", "tele": "xxx", "username": "xxx"} # line.strip().split() turn string line, # example 'foo,baz,bar\n' (trailing new line `strip`ped out) # list ['foo', 'baz', 'bar']. # zipping with, say, ['ln', 'fn', 'email'] yield # [('ln', 'foo'), ('fn', 'baz'), ('email', 'bar')] -- # ie. list of tuples key , value. # can used `dict.update` function replace , augment # default values declared above. vals.update(zip(fields, line.strip().split(","))) # finally, use interpolation operator % merge template # values line , print standard output. print template % vals
Comments
Post a Comment