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.

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

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

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.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.