Python-nameparser use guide: simplify the method of analysis of people in Python
Python-nameparser use guide: simplify the method of analysis of people in Python
In Python programming, sometimes we need to decompose a string containing human names into surnames, names, and intermediate names to better process this information.Python-NameParser is a powerful Python library that can help us achieve this goal.This article will introduce the guidelines of Python-NameParser, as well as related programming code and configuration.
1. Install python-nameparser
Before starting, we first need to install the Python-Nameparser library.You can use the PIP tool to execute the following command for installation:
pip install nameparser
2. Import python-nameparser
After the installation is completed, the nameparser is imported in the Python script to use the functions it provided.Add the following code to the beginning of the script:
python
from nameparser import HumanName
3. Analyze the name
To analyze a person's name string, we need to pass it to the Humanname class of nameparser.For example, suppose we want to analyze the name of "Zhang San":
python
name = Humanname ("Zhang San")
4. Get the results of the analysis
Through the following ways, we can obtain the analysis results of the surname, name and intermediate name:
python
last_name = name.last
first_name = name.first
middle_name = name.middle
The above variables will store the names, names and middle names of the names.If there is no intermediate name in the person's name, the Middle_name variable will be an empty string.
5. Example code
Below is a complete sample code, which shows how to use Python-NameParser to analyze the name:
python
from nameparser import HumanName
def parse_name(full_name):
name = HumanName(full_name)
last_name = name.last
first_name = name.first
middle_name = name.middle
return {
"last_name": last_name,
"first_name": first_name,
"middle_name": middle_name
}
full_name = "Zhang San"
parsed_name = parse_name(full_name)
print("姓:", parsed_name["last_name"])
print("名:", parsed_name["first_name"])
Print ("Intermediate name:", Parsed_name ["Middle_name"]))
The PARSE_NAME function in the above code accepts a person's name string and returns a dictionary containing the analytic results.We can then access the corresponding resolution results through the dictionary key.
6 Conclusion
Using the Python-NameParser library, we can easily analyze the string containing the name of the person as the surname, name, and middle name.By following the use guidelines provided by this article, and using example code, we can quickly get started and realize the function of human name analysis.