Print value from array

M

Mike

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@domain displayName Name SecondName givenName Name sn SecondName cn Name

and i wish

ma "user@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()
 
M

Mike

El jueves, 22 de noviembre de 2012 16:02:30 UTC-3, Alister escribió:
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@domain displayName Name SecondName givenName Name sn SecondName
cn Name

and i wish

ma "user@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()



concatenation is not the python way to build strings

double quotes can be included in single quoted strings ( & vice Versa)

try using the string formatting options, something like





print 'ma "{}" display name "{}" Given Name "{}" sn "{}" cn "{}"'.format

((mail,name2.title(),name.title(),lastname.title(),name.title()))





--

"Plan to throw one away. You will anyway."

- Fred Brooks, "The Mythical Man Month"

Thanks Alister , i modify the print structure with your recommendation and is solved

Thanks.
 
H

Hans Mulder

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@domain displayName Name SecondName givenName Name sn SecondName cn Name

and i wish

ma "user@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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top