简体   繁体   English

从python中的文件中提取名称

[英]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... “ 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! 我不是一个经验丰富的程序员,因此,我的脑海中没有太多的regex术语,因此不胜感激!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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