Skip to main content

A simple Python module for parsing human names into their individual components.

Project description

https://travis-ci.org/derek73/python-nameparser.svg?branch=master https://badge.fury.io/py/nameparser.svg

A simple Python (3.2+ & 2.6+) module for parsing human names into their individual components. Pass the HumanName class a string containing a full name. The name is split on spaces and then parsed into name parts based on placement in the string and matches against known name pieces like titles. Access the name via instance attributes.

It correctly handles some common conjunctions and special prefixes to last names like “del”. Titles can be chained together and include conjunctions to handle titles like “Asst Secretary of State”. It can also try to correct capitalization of all names that are all upper- or lowercase names.

It attempts the best guess that can be made with a simple, rule-based approach. Unicode is supported, but the parser is not likely to be useful for languages that do not have a structure similar to English names. It’s not perfect, but it gets you pretty far.

Quick Start Example

>>> from nameparser import HumanName
>>> name = HumanName("Dr. Juan Q. Xavier de la Vega III (Doc Vega)")
>>> name
<HumanName : [
    title: 'Dr.'
    first: 'Juan'
    middle: 'Q. Xavier'
    last: 'de la Vega'
    suffix: 'III'
    nickname: 'Doc Vega'
]>
>>> name.last
'de la Vega'
>>> name.as_dict()
{'last': 'de la Vega', 'suffix': 'III', 'title': 'Dr.', 'middle': 'Q. Xavier', 'nickname': 'Doc Vega', 'first': 'Juan'}
>>> name.string_format = "{first} {last}"
>>> str(name)
'Juan de la Vega'

3 different comma placement variations are supported for the string that you pass.

  • Title Firstname “Nickname” Middle Middle Lastname Suffix

  • Lastname [Suffix], Title Firstname (Nickname) Middle Middle[,] Suffix [, Suffix]

  • Title Firstname M Lastname [Suffix], Suffix [Suffix] [, Suffix]

The parser does not make any attempt to clean the data. It mostly just splits on white space and puts things in buckets based on their position in the string. This also means the difference between ‘title’ and ‘suffix’ is positional, not semantic. (“Pre-nominal” and “post-nominal” would probably be better names.)

>>> name = HumanName("1 & 2, 3 4 5, Mr.")
>>> name
<HumanName : [
    title: ''
    first: '3'
    middle: '4 5'
    last: '1 & 2'
    suffix: 'Mr.'
    nickname: ''
]>

Customization

Your project may need a bit of adjustments for your dataset. You can do this in your own pre- or post-processing, by customizing the configured pre-defined sets of titles, prefixes, etc., or by subclassing the HumanName class. See the full documentation for more information.

Full documentation

Installation

pip install nameparser

If you want to try out the latest code from GitHub you can install with pip using the command below.

pip install -e git+git://github.com/derek73/python-nameparser.git#egg=nameparser

If you’re looking for a web service, check out eyeseast’s nameparse service, a simple Heroku-friendly Flask wrapper for this module.

Contributing

If you come across name piece that you think should be in the default config, you’re probably right. Start a New Issue and we can get them added.

Please let me know if there are ways this library could be structured to make it easier for you to use in your projects. Read CONTRIBUTING.md for more info on running the tests and contributing to the project.

GitHub Project

https://github.com/derek73/python-nameparser

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nameparser-0.3.12.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nameparser-0.3.12-py2.py3-none-any.whl (23.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nameparser-0.3.12.tar.gz.

File metadata

  • Download URL: nameparser-0.3.12.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nameparser-0.3.12.tar.gz
Algorithm Hash digest
SHA256 4f11ccd2150bb17a67e6571d2b5ea2b1e9c7742ea4dd64ade259be30bc0207da
MD5 214646efbd78da5de6a5aa0258ac4db4
BLAKE2b-256 eb393297944eebdc3f1a308880bc620a8fbff190918ea9c3a4b65b50f3ff35f1

See more details on using hashes here.

File details

Details for the file nameparser-0.3.12-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for nameparser-0.3.12-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1cd6fc6d7bb3a0e56bfa55885ddc459db3bf8a76e2e5a6d09512be0bd123a618
MD5 f145d8aad134b70faa156c523614a6a2
BLAKE2b-256 8918096fdae46f3a7270d869fd90cb98491cba03f30fd39aebc974d4c7d0c3fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page