简体   繁体   中英

Extract names from file in python

I'm dealing with a file where I want to extract the names and append them to a list. The names are formatted like this:

"Firstname LastnameAnotherfirstname Anotherlastname" etc...

I'm not an experienced programmer, therefore, I haven't got a lot of regex lingo in the back of my head so a little help would be much appreciated!

Find name by Upper Case

import re

file_open = open('file_name.txt').read()
value = re.findall('[A-Z][a-z]*', file_open)
print(value)

You get a list of name.

Try like this

import re
regex = r"[A-Z][a-z]*\ [A-Z][a-z]*"
test_str = "Firstname LastnameAnotherfirstname Anotherlastname"
output = re.findall(regex, test_str)
print output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM