Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String extraction
#1
Hi everyone,

I am looking at extracting name titles and putting them into their own column in a dataset.

The names are in this format: grant-perice, Mr. Owen Harris.

Can I start a string search from the , and end at the . and extract into a column and delete any white space? Perhaps with the extract method?

Thanks
Reply
#2
>>> my_string = 'grant-perice, Mr. Owen Harris'
>>> title, name = my_string.split(',')
>>> title
'grant-perice'
>>> name.strip()
'Mr. Owen Harris'
>>> name.split('.')[-1].strip()
'Owen Harris'
>>> title, name = my_string.split('.')
>>> name.strip()
'Owen Harris'
>>> title
'grant-perice, Mr'
of course, you can also use RegEx, check re module

or you can install third party package nameparser
>>> import nameparser
>>> name = HumanName('grant-perice, Mr. Owen Harris')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'HumanName' is not defined
>>> name = nameparser.HumanName('grant-perice, Mr. Owen Harris')
>>> name
<HumanName : [
	title: 'Mr.' 
	first: 'Owen' 
	middle: 'Harris' 
	last: 'grant-perice' 
	suffix: ''
	nickname: ''
]>
>>> name = nameparser.HumanName('Mr. Owen Harris')
>>> name
<HumanName : [
	title: 'Mr.' 
	first: 'Owen' 
	middle: '' 
	last: 'Harris' 
	suffix: ''
	nickname: ''
]>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
That is just one example there is a column called 'Names' with over 100 entries.
Reply
#4
(Jul-21-2018, 10:49 AM)Scott Wrote: That is just one example there is a column called 'Names' with over 100 entries.
yes, that is example. Now start coding and implement same in a loop. We are not going to do it for you.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Data extraction from a table based on column and row names tgottsc1 1 2,356 Jan-09-2021, 10:04 PM
Last Post: buran
  eml file data extraction ajetrumpet 2 2,569 Jul-04-2020, 04:34 AM
Last Post: ajetrumpet
  Table extraction from scanned PDF RupamKundu 1 3,654 Aug-03-2019, 02:59 AM
Last Post: Larz60+
  Json value extraction aaronwarwick 1 2,103 Jun-24-2019, 07:23 PM
Last Post: micseydel
  Substring extraction nevendary 6 3,888 Apr-24-2019, 05:41 AM
Last Post: nevendary
  Automating a Data Extraction Process Harrison 12 8,472 Mar-31-2017, 10:44 AM
Last Post: Harrison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020