分离名字、姓和中间名(Python)

3 投票
8 回答
11425 浏览
提问于 2025-04-16 09:53

我有一份包含几百个成员的名单,我想把他们的名字分开,分别是名字、第二名字和姓氏。不过,有些成员的名字前面有前缀(用'P'表示)。所有可能的组合如下:

First Middle Last
P First Middle Last
First P Middle Last
P First p Middle Last

我想知道怎么在Python中把名字(如果有前缀就带上)、第二名字(如果有前缀就带上)和姓氏分开。我写了一些代码,但效果不是很好。

import csv
inPath = "input.txt"
outPath = "output.txt"

newlist = []

file = open(inPath, 'rU')
if file:
    for line in file:
        member = line.split()
        newlist.append(member)
    file.close()
else:
    print "Error Opening File."

file = open(outPath, 'wb')
if file:
    for i in range(len(newlist)):
        print i, newlist[i][0] # Should get the First Name with Prefix
        print i, newlist[i][1] # Should get the Middle Name with Prefix
        print i, newlist[i][-1]
    file.close()
else:
    print "Error Opening File."

我想要的结果是:

  1. 提取名字和第二名字,并且如果有前缀就带上前缀
  2. 把每个名字(名字、第二名字、姓氏)输出到单独的文本文件,或者最好是一个CSV文件。

非常感谢你的帮助。

8 个回答

1
names = [('A', 'John', 'Paul', 'Smith'),
('Matthew', 'M', 'Phil', 'Bond'),
('A', 'Morris', 'O', 'Reil', 'M', 'Big')]

def getItem():
    for name in names:
        for (pos,item) in enumerate(name):
            yield item

itembase = getItem()

for i in enumerate(names):
    element = itembase.next()
    if len(element) == 1: firstName = element+" "+itembase.next()
    else: firstName = element
    element = itembase.next()
    if len(element) == 1: mName = element+" "+itembase.next()
    else: mName = element
    element = itembase.next()
    if len(element) == 1: lastName = element+" "+itembase.next()
    else: lastName = element

    print "First Name: "+firstName
    print "Middle Name: "+mName
    print "Last Name: "+lastName
    print "--"
**Output**
First Name: A John
Middle Name: Paul
Last Name: Smith

First Name: Matthew
Middle Name: M Phil
Last Name: Bond

First Name: A Morris
Middle Name: O Reil
Last Name: M Big

这段话的意思是,这个方法看起来有效。把 len(element) == 1 这个条件替换掉(我之前不知道你需要检查的只有三个,所以我做了一个检查任何单个字母的条件)改成你需要的三个前缀的条件。

2

这是一个面向对象的方式:

class Name(object):
    def __init__(self, fullname):
        self.full = fullname
        s = self.full.split()

        try:
            self.first = " ".join(s[:2]) if len(s[0]) == 1 else s[0]
            s = s[len(self.first.split()):]

            self.middle = " ".join(s[:2]) if len(s[0]) == 1 else s[0]
            s = s[len(self.middle.split()):]

            self.last = " ".join(s[:2]) if len(s[0]) == 1 else s[0]
        finally:
            pass

names = [
    "First Middle Last",
    "P First Middle Last",
    "First P Middle Last",
    "P First p Middle Last",
]

for fullname in names:
    name = Name(fullname)
    print (name.first, name.middle, name.last)
2

下面是一个完整的测试脚本:

import sys

def process(file):
    for line in file:
        arr = line.split()
        if not arr:
            continue
        last = arr.pop()
        n = len(arr)
        if n == 4:
            first, middle = ' '.join(arr[:2]), ' '.join(arr[2:])
        elif n == 3:
            if arr[0] in ('M', 'Shk', 'BS'):
                first, middle = ' '.join(arr[:2]), arr[-1]
            else:
                first, middle = arr[0], ' '.join(arr[1:])
        elif n == 2:
            first, middle = arr
        else:
            continue
        print 'First: %r' % first
        print 'Middle: %r' % middle
        print 'Last: %r' % last

if __name__ == '__main__':
    process(sys.stdin)

如果你在Linux系统上运行这个脚本,可以输入示例行,然后按Ctrl+D来表示输入结束。在Windows系统上,要用Ctrl+Z来代替Ctrl+D。当然,你也可以直接把一个文件传给它。

下面这个输入文件:

First Middle Last
M First Middle Last
First Shk Middle Last
BS First M Middle Last

会产生这样的输出:

First: 'First'
Middle: 'Middle'
Last: 'Last'
First: 'M First'
Middle: 'Middle'
Last: 'Last'
First: 'First'
Middle: 'Shk Middle'
Last: 'Last'
First: 'BS First'
Middle: 'M Middle'
Last: 'Last'

撰写回答