Monday, 19 August 2013

Python: Using CSV to parse(?) variables and then output that to another file

Python: Using CSV to parse(?) variables and then output that to another file

I am a Server Administrator. I've skated by without having to do much
scripting, but alas -- it has reared its ugly head.
Summary: I have example.csv that looks like the 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 to read in the csv file. Then, I want to take the value
from each row and put it into something like 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 you can see, I'm attempting to make an LDIF file that allows me to
import user names and then auto fill in the variables.
I just can't seem to put the pieces together.
I haven't gotten very far either. I learned to print rows, yay... !
import csv
with open('example.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row
I think the logic is as follows.
Import .CSV. Loop through Row.
Place the Data into Variables.
Output the final product (print?) into "Output_File"
Loop until EOF?
Any help would be appreciated.

No comments:

Post a Comment