Print value from array

Hans Mulder hansmu at xs4all.nl
Fri Nov 23 10:22:13 CET 2012


On 22/11/12 19:44:02, Mike wrote:
> Hello,
> I am noob en python programing,  i wrote a perl script for read from csv but now i wish print value but the value must be within double quote and I can not do this. 
> 
> For example now the output is:
> 
> ma user at domain displayName Name SecondName givenName Name sn SecondName cn Name
> 
> and i wish
> 
> ma "user at domain" displayName "Name Lastname" givenName "Name" sn "SecondName" cn "Name"
> 
> My script is
> 
> #!/usr/bin/python
> import csv
> 
> with open ('file.csv', 'rb') as f:
> 	reader = csv.reader (f, delimiter=';' )
> 	for row in reader:
> 		mail 	 = row [0]
> 		name 	 = row [1]
> 		lastname = row [2]
> 		name2  = row [1] + ' ' + row [2]
> 		
> 		print  'ma ' + mail + ' displayName ' +  name2.title() + ' givenName ' + name.title() + ' sn ' + lastname.title() + ' cn ' + name.title()  
> 
> 	#	print '\n'
> 
> f.close() 

How about:

#!/usr/bin/python
import csv

with open('file.csv', 'rb') as f:
        reader = csv.reader(f, delimiter=';' )
        for mail, firstname, lastname in reader:
                fullname  = firstname + ' ' + lastname

                print 'ma "%s"' % mail,
                print 'displayname "%s"' % fullname.title(),
                print 'givenName "%s"' % firstname.title(),
                print 'sn "%s"' % lastname.title(),
                print 'cn "%s"' % firstname.title()

f.close()


-- HansM


More information about the Python-list mailing list