Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

shortening firstname.lastname to flastname

Posted on 2004-09-01
4
Medium Priority
?
329 Views
Last Modified: 2010-08-05
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
Comment
Question by:metrion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 1000 total points
ID: 11958186
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
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11961666
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
 

Author Comment

by:metrion
ID: 11965562
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
 
LVL 9

Expert Comment

by:rjkimble
ID: 11966930
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question