Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

shortening firstname.lastname to flastname

Hi all,

I have a list of names. one per line, that I need to shorten. Each name is in the form of firstname.lastname, and I need to have them in flastname form. So john.doe would be jdoe. I'm sure this is pretty basic but I can't figure it out :P


Also, I've never asked a question before. Since I don't know python and can't figure it out, does that mean it's difficult (250 points)? I'm sure it's a snap for someone, hehe. I'll assign something now and know better for next time :)

Thanks!

M
0
metrion
Asked:
metrion
  • 2
1 Solution
 
rjkimbleCommented:
Suppose you have your list of names in a file, names.txt. You can read them into a list using something like this:

file( 'names.txt', 'r' ).readlines( )

Assuming that there are no exceptions to your rule in the file, you can create a list in "flastname" format using something like this:

flastnames = [ first[0] + last for first, last in [ name.split( '.' ) for name in file( 'names.txt', 'r' ).readlines( ) ] ]

You can also break that up a bit:

names = file( 'names.txt', 'r' ).readlines( )
flastnames = list( )
for name in names:
    first, last = name.split( '.' )
    flastnames.append( first[0] + last )

0
 
cjjcliffordCommented:
slightly compressed version of the first command would be:

flastnames = [ name[0] + name.split(')[1] for name in file( 'names.txt', 'r' ).readlines( ) ] ]

the broken up would become:

names = file( 'names.txt', 'r' ).readlines( )
flastnames = []
for name in names:
    flastnames.append( name[0] + name.split('.')[1] )


(basically, removed the explit naming of the results of the split) - definitely give rjkimble the points though, just posted this for interest sakes!
0
 
metrionAuthor Commented:
Wow, that's excellent! Worked like a charm, thanks! (yours too cjjclifford!)

One thing, it leaves an empty line between each result when I print it out like so:

#!/usr/bin/python

names = file( 'aubnames.txt', 'r' ).readlines( )
flastnames = []
for name in names:
        flastnames.append( name[0] + name.split('.')[1] )
        print name

Is that not the correct way to dump results to stdout?
0
 
rjkimbleCommented:
readlines() leaves the line endings. You can try this instead:

#!/usr/bin/python

names = file( 'aubnames.txt', 'r' ).read( ).splitlines( )
flastnames = []
for name in names:
        flastnames.append( name[0] + name.split('.')[1] )
        print name

This approach reads the entire file into a string and then uses the splitlines() method to break it into a list of lines. The splitlines() method defaults to removing line endings.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now