从python中的文件中提取名称

时间:2023-02-06 10:42:02

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...

“Firstname LastnameAnotherfirstname Anotherlastname”等...

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!

我不是一个经验丰富的程序员,因此,我的脑后没有很多正则表达式,所以我会非常感激你的帮助!

2 个解决方案

#1


0  

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.

你得到一个名单。

#2


0  

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

#1


0  

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.

你得到一个名单。

#2


0  

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