We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,254 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Rearrange names from file

I have a problem that I have been working on. I felt like I was getting close just to find out with wasn't the 100% correct way of doing it. What I need to do is take a list of names from a file in the format last, first, middle. Here is one way I tried to do it.

The names in the txt file are listed as:

Neuman, Alfred E.
Stevenson, Robert Lewis
Lewis, C.S.
Doe, Jane
Bush, George Herbert Walker

Edit: It doesn't show, but there are 3 spaces in between Herbert and Walker

names = open("Names.txt", 'rU')
for name in names:
	s = name.split(",")
	newNames=s[1].strip() +" "+ s[0].strip()
	print newNames

When I print it, the output is:

Alfred E. Neuman
Robert Lewis Stevenson
C.S. Lewis
Jane Doe
George Herbert Walker Bush

Edit: Also still 3 spaces between Herbert and Walker

It looks alright except I need to make it so the whitespace between 'Herbert' and 'Walker' is just a single space. Also it needs to be able to handle even more middle names. I was told to perhaps not split it at the "," which makes sense. I'm just not sure what to do from there.

3
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
python-noob
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

strip removes those characters from the left and right sides of the string. If the character occurs in the middle of the string it will have no effect on it. This is what I use to remove duplicate characters from a string

def Unduplicate(string, chars):
    for char in chars:
        string = char.join([x for x in string.split(char) if x])
    return string

your new code would look like

names = open("Names.txt", 'rU')
for name in names:
    # chr(32) is a space
    name = Unduplicate(name, chr(32))
    s = name.split(",")
    newNames=s[1].strip() +" "+ s[0].strip()
    print newNames
ihatehippies
Junior Poster
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

Maybe this will help you:

filename='name.txt'
fh = open(filename, 'r')
line = open(filename).read().splitlines()
for ln in line:
    print ln
"""
My output:
Neuman, Alfred E.
Stevenson, Robert Lewis 
Lewis, C.S. 
Doe, Jane 
Bush, George Herbert Walker
"""

It should handle middle names with no problem, I guess...:D

Lucaci Andrew
Practically a Master Poster
649 posts since Jan 2012
Reputation Points: 91
Solved Threads: 91
Skill Endorsements: 12

strip removes those characters from the left and right sides of the string. If the character occurs in the middle of the string it will have no effect on it. This is what I use to remove duplicate characters from a string

def Unduplicate(string, chars):
    for char in chars:
        string = char.join([x for x in string.split(char) if x])
    return string

your new code would look like

names = open("Names.txt", 'rU')
for name in names:
    # chr(32) is a space
    name = Unduplicate(name, chr(32))
    s = name.split(",")
    newNames=s[1].strip() +" "+ s[0].strip()
    print newNames

This works perfectly, thank you. But I am trying to find a way to do it without having to define a function.

python-noob
Newbie Poster
11 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by ihatehippies and Lucaci Andrew

Then just copy the contents of the Unduplicate function (without the return statement) and change the variable names.

names = open("Names.txt", 'rU')
for name in names:
    name = ' '.join([x for x in name.split(' ') if x])
    s = name.split(",")
    newNames=s[1].strip() +" "+ s[0].strip()
    print newNames
ihatehippies
Junior Poster
197 posts since Oct 2008
Reputation Points: 33
Solved Threads: 16
Skill Endorsements: 1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2980 seconds using 2.66MB