将一个人的名字拆分为姓氏和姓氏

时间:2022-09-13 10:49:43

ok so basically I am asking the question of their name I want this to be one input rather than Forename and Surname.

好吧基本上我问的是他们的名字问题我希望这是一个输入而不是Forename和Surname。

Now is there any way of splitting this name? and taking just the last word from the "Sentence" e.g.

现在有没有办法拆分这个名字?并从“句子”中取出最后一个字,例如

name = "Thomas Winter"
print name.split() 

and what would be output is just "Winter"

什么输出只是“冬天”

14 个解决方案

#1


62  

You'll find that your key problem with this approach isn't a technical one, but a human one - different people write their names in different ways.

你会发现你使用这种方法的关键问题不是技术问题,而是人类问题 - 不同的人以不同的方式写出自己的名字。

In fact, the terminology of "forename" and "surname" is itself flawed.

事实上,“姓”和“姓”的术语本身就存在缺陷。

While many blended families use a hyphenated family name, such as Smith-Jones, there are some who just use both names separately, "Smith Jones" where both names are the family name.

虽然许多混合家庭使用带连字符的姓氏,例如Smith-Jones,但有些人只是单独使用这两个名字,“Smith Jones”,其中两个名字都是姓氏。

Many european family names have multiple parts, such as "de Vere" and "van den Neiulaar". Sometimes these extras have important family history - for example, a prefix awarded by a king hundreds of years ago.

许多欧洲家族的名字有多个部分,例如“de Vere”和“van den Neiulaar”。有时这些额外的东西有重要的家族历史 - 例如,数百年前由国王授予的前缀。

Side issue: I've capitalised these correctly for the people I'm referencing - "de" and "van den" don't get captial letters for some families, but do for others.

侧面问题:我已经正确地将这些资本化为我所引用的人 - “de”和“van den”不会为某些家庭获得大写字母,而是为其他人做。

Conversely, many Asian cultures put the family name first, because the family is considered more important than the individual.

相反,许多亚洲文化将姓氏放在首位,因为家庭被认为比个人更重要。

Last point - some people place great store in being "Junior" or "Senior" or "III" - and your code shouldn't treat those as the family name.

最后一点 - 有些人非常喜欢“少年”或“高级”或“III” - 你的代码不应该把它们视为姓氏。

Also noting that there are a fair number of people who use a name that isn't the one bestowed by their parents, I've used the following scheme with some success:

还注意到有相当多的人使用的名称不是他们父母赋予的名字,我使用了以下方案并取得了一些成功:

Full Name (as normally written for addressing mail); Family Name; Known As (the name commonly used in conversation).

全名(通常用于处理邮件);姓;已知为(会话中常用的名称)。

e.g:

Full Name: William Gates III; Family Name: Gates; Known As: Bill

全名:William Gates III;姓氏:盖茨;众所周知:比尔

Full Name: Soong Li; Family Name: Soong; Known As: Lisa

全名:宋李;姓氏:宋;被称为:丽莎

#2


14  

The problem with trying to split the names from a single input is that you won't get the full surname for people with spaces in their surname, and I don't believe you'll be able to write code to manage that completely.

尝试从单个输入中拆分名称的问题在于,对于姓氏中包含空格的人,您将无法获得完整的姓氏,我不相信您将能够编写代码来完全管理它。

I would recommend that you ask for the names separately if it is at all possible.

如果可能的话,我建议您单独询问名称。

#3


9  

This is a pretty old issue but I found it searching around for a solution to parsing out pieces from a globbed together name.

这是一个相当古老的问题,但我发现它正在寻找一种解决方案来解析整体名称中的碎片。

http://code.google.com/p/python-nameparser/

#4


7  

Golden rule of data - don't aggregate too early - it is much easier to glue fields together than separate them. Most people also have a middle name which should be an optional field. Some people have a plethora of middle names. Some people only have one name, one word. Some cultures commonly have a dictionary of middle names, paying homage to the family tree back to the Golgafrincham Ark landing.

数据的黄金法则 - 不要过早聚合 - 将字段粘合在一起比分离它们要容易得多。大多数人还有一个中间名,应该是一个可选字段。有些人有很多中间人。有些人只有一个名字,一个字。一些文化通常都有一个中间名字典,向家族树致敬,回到Golgafrincham Ark登陆。

You don't need a code solution here - you need a business rule.

您不需要代码解决方案 - 您需要业务规则。

#5


5  

An easy way to do exactly what you asked in python is

一个简单的方法来完成你在python中所要求的是

name = "Thomas Winter"
LastName = name.split()[1]

(note the parantheses on the function call split.)

(注意函数调用拆分的parantheses。)

split() creates a list where each element is from your original string, delimited by whitespace. You can now grab the second element using name.split()[1] or the last element using name.split()[-1]

split()创建一个列表,其中每个元素都来自原始字符串,由空格分隔。您现在可以使用name.split()[1]获取第二个元素,或使用name.split()[ - 1]获取最后一个元素

However, as others said, unless you're SURE you're just getting a string like "First_Name Last_Name", there are a lot more issues involved.

然而,正如其他人所说,除非你确定你只是得到一个像“First_Name Last_Name”这样的字符串,否则涉及的问题会更多。

#6


4  

If you're trying to parse apart a human name in PHP, I recomment Keith Beckman's nameparse.php script.

如果你试图用PHP解析一个人名,我推荐Keith Beckman的nameparse.php脚本。

#7


4  

This is how I do it in my application:

这就是我在我的应用程序中的做法:

def get_first_name(fullname):
    firstname = ''
    try:
        firstname = fullname.split()[0] 
    except Exception as e:
        print str(e)
    return firstname

def get_last_name(fullname):
    lastname = ''
    try:
        index=0
        for part in fullname.split():
            if index > 0:
                if index > 1:
                    lastname += ' ' 
                lastname +=  part
            index += 1
    except Exception as e:
            print str(e)
    return lastname

def get_last_word(string):
    return string.split()[-1]

print get_first_name('Jim Van Loon')
print get_last_name('Jim Van Loon')
print get_last_word('Jim Van Loon')

#8


2  

Splitting names is harder than it looks. Some names have two word last names; some people will enter a first, middle, and last name; some names have two work first names. The more reliable (or least unreliable) way to handle names is to always capture first and last name in separate fields. Of course this raises its own issues, like how to handle people with only one name, making sure it works for users that have a different ordering of name parts.

拆分名称比看起来更难。有些名字有两个字的姓氏;有些人会输入第一,中间和姓氏;有些名字有两个名字。处理名称的更可靠(或最不可靠)的方法是始终在单独的字段中捕获名字和姓氏。当然,这会引发自己的问题,例如如何处理只有一个名字的人,确保它适用于具有不同名称部分顺序的用户。

Names are hard, handle with care.

名字很难,小心处理。

#9


2  

Like this:

print name.split()[-1]

#10


2  

Since there are so many different variation's of how people write their names, but here's how a basic way to get the first/lastname via regex.

由于人们如何写出他们的名字有很多不同的变化,但这里是通过正则表达式获得第一个/最后一个名字的基本方法。

import re
p = re.compile(r'^(\s+)?(Mr(\.)?|Mrs(\.)?)?(?P<FIRST_NAME>.+)(\s+)(?P<LAST_NAME>.+)$', re.IGNORECASE)
m = p.match('Mr. Dingo Bat')
if(m != None):
  first_name = m.group('FIRST_NAME')
  last_name = m.group('LAST_NAME')

#11


1  

It's definitely a more complicated task than it appears on the surface. I wrote up some of the challenges as well as my algorithm for solving it on my blog. Be sure to check out my Google Code project for it if you want the latest version in PHP:

这绝对是一个比表面上看起来更复杂的任务。我写了一些挑战以及我在博客上解决它的算法。如果您想要PHP中的最新版本,请务必查看我的Google代码项目:

http://www.onlineaspect.com/2009/08/17/splitting-names/

#12


0  

you'd probably want to use rsplit for this:

你可能想要使用rsplit:

rsplit([sep [,maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below. New in version 2.4.

使用sep作为分隔符字符串,返回字符串中单词的列表。如果给出maxsplit,则最多完成maxsplit分割,最右边的分割。如果未指定sep或None,则任何空格字符串都是分隔符。除了从右边分割之外,rsplit()的行为类似于split(),下面将详细介绍。版本2.4中的新功能。

#13


0  

Here's how to do it in SQL. But data normalization with this kind of thing is really a bear. I agree with Dave DuPlantis about asking for separate inputs.

这是在SQL中如何做到这一点。但是用这种东西进行数据规范化确实是一个负担。我同意Dave DuPlantis关于要求单独输入的意见。

#14


0  

I would specify a standard format (some forms use them), such as "Please write your name in First name, Surname form".

我会指定一种标准格式(某些表格使用它们),例如“请在名字,姓氏表格中写下你的名字”。

It makes it easier for you, as names don't usually contain a comma. It also verifies that your users actually enter both first name and surname.

它使您更容易,因为名称通常不包含逗号。它还会验证您的用户是否实际输入了名字和姓氏。

#1


62  

You'll find that your key problem with this approach isn't a technical one, but a human one - different people write their names in different ways.

你会发现你使用这种方法的关键问题不是技术问题,而是人类问题 - 不同的人以不同的方式写出自己的名字。

In fact, the terminology of "forename" and "surname" is itself flawed.

事实上,“姓”和“姓”的术语本身就存在缺陷。

While many blended families use a hyphenated family name, such as Smith-Jones, there are some who just use both names separately, "Smith Jones" where both names are the family name.

虽然许多混合家庭使用带连字符的姓氏,例如Smith-Jones,但有些人只是单独使用这两个名字,“Smith Jones”,其中两个名字都是姓氏。

Many european family names have multiple parts, such as "de Vere" and "van den Neiulaar". Sometimes these extras have important family history - for example, a prefix awarded by a king hundreds of years ago.

许多欧洲家族的名字有多个部分,例如“de Vere”和“van den Neiulaar”。有时这些额外的东西有重要的家族历史 - 例如,数百年前由国王授予的前缀。

Side issue: I've capitalised these correctly for the people I'm referencing - "de" and "van den" don't get captial letters for some families, but do for others.

侧面问题:我已经正确地将这些资本化为我所引用的人 - “de”和“van den”不会为某些家庭获得大写字母,而是为其他人做。

Conversely, many Asian cultures put the family name first, because the family is considered more important than the individual.

相反,许多亚洲文化将姓氏放在首位,因为家庭被认为比个人更重要。

Last point - some people place great store in being "Junior" or "Senior" or "III" - and your code shouldn't treat those as the family name.

最后一点 - 有些人非常喜欢“少年”或“高级”或“III” - 你的代码不应该把它们视为姓氏。

Also noting that there are a fair number of people who use a name that isn't the one bestowed by their parents, I've used the following scheme with some success:

还注意到有相当多的人使用的名称不是他们父母赋予的名字,我使用了以下方案并取得了一些成功:

Full Name (as normally written for addressing mail); Family Name; Known As (the name commonly used in conversation).

全名(通常用于处理邮件);姓;已知为(会话中常用的名称)。

e.g:

Full Name: William Gates III; Family Name: Gates; Known As: Bill

全名:William Gates III;姓氏:盖茨;众所周知:比尔

Full Name: Soong Li; Family Name: Soong; Known As: Lisa

全名:宋李;姓氏:宋;被称为:丽莎

#2


14  

The problem with trying to split the names from a single input is that you won't get the full surname for people with spaces in their surname, and I don't believe you'll be able to write code to manage that completely.

尝试从单个输入中拆分名称的问题在于,对于姓氏中包含空格的人,您将无法获得完整的姓氏,我不相信您将能够编写代码来完全管理它。

I would recommend that you ask for the names separately if it is at all possible.

如果可能的话,我建议您单独询问名称。

#3


9  

This is a pretty old issue but I found it searching around for a solution to parsing out pieces from a globbed together name.

这是一个相当古老的问题,但我发现它正在寻找一种解决方案来解析整体名称中的碎片。

http://code.google.com/p/python-nameparser/

#4


7  

Golden rule of data - don't aggregate too early - it is much easier to glue fields together than separate them. Most people also have a middle name which should be an optional field. Some people have a plethora of middle names. Some people only have one name, one word. Some cultures commonly have a dictionary of middle names, paying homage to the family tree back to the Golgafrincham Ark landing.

数据的黄金法则 - 不要过早聚合 - 将字段粘合在一起比分离它们要容易得多。大多数人还有一个中间名,应该是一个可选字段。有些人有很多中间人。有些人只有一个名字,一个字。一些文化通常都有一个中间名字典,向家族树致敬,回到Golgafrincham Ark登陆。

You don't need a code solution here - you need a business rule.

您不需要代码解决方案 - 您需要业务规则。

#5


5  

An easy way to do exactly what you asked in python is

一个简单的方法来完成你在python中所要求的是

name = "Thomas Winter"
LastName = name.split()[1]

(note the parantheses on the function call split.)

(注意函数调用拆分的parantheses。)

split() creates a list where each element is from your original string, delimited by whitespace. You can now grab the second element using name.split()[1] or the last element using name.split()[-1]

split()创建一个列表,其中每个元素都来自原始字符串,由空格分隔。您现在可以使用name.split()[1]获取第二个元素,或使用name.split()[ - 1]获取最后一个元素

However, as others said, unless you're SURE you're just getting a string like "First_Name Last_Name", there are a lot more issues involved.

然而,正如其他人所说,除非你确定你只是得到一个像“First_Name Last_Name”这样的字符串,否则涉及的问题会更多。

#6


4  

If you're trying to parse apart a human name in PHP, I recomment Keith Beckman's nameparse.php script.

如果你试图用PHP解析一个人名,我推荐Keith Beckman的nameparse.php脚本。

#7


4  

This is how I do it in my application:

这就是我在我的应用程序中的做法:

def get_first_name(fullname):
    firstname = ''
    try:
        firstname = fullname.split()[0] 
    except Exception as e:
        print str(e)
    return firstname

def get_last_name(fullname):
    lastname = ''
    try:
        index=0
        for part in fullname.split():
            if index > 0:
                if index > 1:
                    lastname += ' ' 
                lastname +=  part
            index += 1
    except Exception as e:
            print str(e)
    return lastname

def get_last_word(string):
    return string.split()[-1]

print get_first_name('Jim Van Loon')
print get_last_name('Jim Van Loon')
print get_last_word('Jim Van Loon')

#8


2  

Splitting names is harder than it looks. Some names have two word last names; some people will enter a first, middle, and last name; some names have two work first names. The more reliable (or least unreliable) way to handle names is to always capture first and last name in separate fields. Of course this raises its own issues, like how to handle people with only one name, making sure it works for users that have a different ordering of name parts.

拆分名称比看起来更难。有些名字有两个字的姓氏;有些人会输入第一,中间和姓氏;有些名字有两个名字。处理名称的更可靠(或最不可靠)的方法是始终在单独的字段中捕获名字和姓氏。当然,这会引发自己的问题,例如如何处理只有一个名字的人,确保它适用于具有不同名称部分顺序的用户。

Names are hard, handle with care.

名字很难,小心处理。

#9


2  

Like this:

print name.split()[-1]

#10


2  

Since there are so many different variation's of how people write their names, but here's how a basic way to get the first/lastname via regex.

由于人们如何写出他们的名字有很多不同的变化,但这里是通过正则表达式获得第一个/最后一个名字的基本方法。

import re
p = re.compile(r'^(\s+)?(Mr(\.)?|Mrs(\.)?)?(?P<FIRST_NAME>.+)(\s+)(?P<LAST_NAME>.+)$', re.IGNORECASE)
m = p.match('Mr. Dingo Bat')
if(m != None):
  first_name = m.group('FIRST_NAME')
  last_name = m.group('LAST_NAME')

#11


1  

It's definitely a more complicated task than it appears on the surface. I wrote up some of the challenges as well as my algorithm for solving it on my blog. Be sure to check out my Google Code project for it if you want the latest version in PHP:

这绝对是一个比表面上看起来更复杂的任务。我写了一些挑战以及我在博客上解决它的算法。如果您想要PHP中的最新版本,请务必查看我的Google代码项目:

http://www.onlineaspect.com/2009/08/17/splitting-names/

#12


0  

you'd probably want to use rsplit for this:

你可能想要使用rsplit:

rsplit([sep [,maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below. New in version 2.4.

使用sep作为分隔符字符串,返回字符串中单词的列表。如果给出maxsplit,则最多完成maxsplit分割,最右边的分割。如果未指定sep或None,则任何空格字符串都是分隔符。除了从右边分割之外,rsplit()的行为类似于split(),下面将详细介绍。版本2.4中的新功能。

#13


0  

Here's how to do it in SQL. But data normalization with this kind of thing is really a bear. I agree with Dave DuPlantis about asking for separate inputs.

这是在SQL中如何做到这一点。但是用这种东西进行数据规范化确实是一个负担。我同意Dave DuPlantis关于要求单独输入的意见。

#14


0  

I would specify a standard format (some forms use them), such as "Please write your name in First name, Surname form".

我会指定一种标准格式(某些表格使用它们),例如“请在名字,姓氏表格中写下你的名字”。

It makes it easier for you, as names don't usually contain a comma. It also verifies that your users actually enter both first name and surname.

它使您更容易,因为名称通常不包含逗号。它还会验证您的用户是否实际输入了名字和姓氏。