Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中从字符串中提取名称_Python_Regex_String - Fatal编程技术网

在python中从字符串中提取名称

在python中从字符串中提取名称,python,regex,string,Python,Regex,String,首先,其目的是仅将名称与由朝鲜语名称、英语名称、特殊字符(-、*、逗号)、空格等组成的字符串区分开来,如果名称重复,则只留下一个 所以,到目前为止,我所做的是将一个文本文件转换成字符串,消除了不必要的特殊字符 import re path = 'E:\Data Science\Personal_Project\Church\Data\original.txt' def open_text(path): with open(path, "r", encoding='euc-kr') a

首先,其目的是仅将名称与由朝鲜语名称、英语名称、特殊字符(-、*、逗号)、空格等组成的字符串区分开来,如果名称重复,则只留下一个

所以,到目前为止,我所做的是将一个文本文件转换成字符串,消除了不必要的特殊字符

import re

path = 'E:\Data Science\Personal_Project\Church\Data\original.txt'

def open_text(path):
    with open(path, "r", encoding='euc-kr') as f:
        text = f.readlines()
        string = ''.join(text)
        unicode_line = string.translate({ord(c): None for c in '.;*\n'})
        cleaned = re.split('-|', unicode_line)


print(unicode_line, type(cleaned))
return(cleaned)
问题是。我想在上面的函数中添加什么

1) 如果虚线前面有一个字母(例如,“考勤---”),我希望在将其拆分为虚线之前删除它前面的文本(例如,“考勤”)

2) 或者我想列一个清单-【出勤、退房、度假】-我想删除清单中的单词

如果你能告诉我一个更好的方法或者一个更像蟒蛇的方法,我将不胜感激

为方便起见,我将添加一个示例文本

Status of January 20th




** Attendance
-----------

John Smith, John Smith, Bob Smith, Mike Smith, Jane Jones, Daniel Lee, Dong Jones, Jeannie Jones, Jessica Yi, McAleer Chung, Shu K Smith, Song Kim, Steve Carlos, Bob Smith





** Absent
---------

holiday, unauthorized, unpaid leave, emergency
------------------------------------------------------------------------------------------- 
Brown Williams, Paul Garcia
另外,这里是我想要的输出,只有未重复的名称。如果你看到上面,有两个约翰·史密斯和两个鲍勃·史密斯。最后,如果我能按字母顺序得到它,那就太棒了

Output:


John Smith, Bob Smith, Mike Smith, Jane Jones, Daniel Lee, Dong Jones, Jeannie Jones, Jessica Yi, McAleer Chung, Shu K Smith, Song Kim, Steve Carlos, Brown Williams, Paul Garcia

如果我理解正确,您希望获得文档中所有名称的
,在某些标题行中没有单词,在预定义的非名称单词列表中没有单词,如“假期”

首先,我建议不要加入所有行,然后您可以检查一行是否以
-
*
开头,并排除该行。这也使得跳过标题的第一行变得更容易。然后,您可以定义非姓名单词列表,在文件中的行上循环并按
拆分

non_names = set("holiday, unauthorized, unpaid leave, emergency".split(", "))
with open("text.txt") as f:
    next(f) # skip first line
    names = set()
    for line in f:
        if not line.startswith(("*", "-")):
            for name in line.strip().split(", "):
                if name and name not in non_names:
                    names.add(name)
或者直接在复杂生成器表达式上使用
set

    names = set(name for line in f
                     if not line.startswith(("*", "-"))
                     for name in line.strip().split(", ")
                     if name and name not in non_names)
无论是哪种方式,结果都是
{'John Smith'、'Jeannie Jones'、'Mike Smith'、'Bob Smith'、'McAleer Chung'、'Steve Carlos'、'Brown Williams'、'Jessica Yi'、'Paul Garcia'、'Jane Jones'、'Shu K Smith'、'Song Kim'、'Daniel le。要获得已排序的名称,只需对
集合进行排序
,或者如果要按姓氏排序,请使用特殊的
功能:

names = sorted(names, key=lambda s: s.split()[-1])

如果我理解正确,您希望获得文档中所有名称的
,在某些标题行中没有单词,在预定义的非名称单词列表中没有单词,如“假期”

首先,我建议不要加入所有行,然后您可以检查一行是否以
-
*
开头,并排除该行。这也使得跳过标题的第一行变得更容易。然后,您可以定义非姓名单词列表,在文件中的行上循环并按
拆分

non_names = set("holiday, unauthorized, unpaid leave, emergency".split(", "))
with open("text.txt") as f:
    next(f) # skip first line
    names = set()
    for line in f:
        if not line.startswith(("*", "-")):
            for name in line.strip().split(", "):
                if name and name not in non_names:
                    names.add(name)
或者直接在复杂生成器表达式上使用
set

    names = set(name for line in f
                     if not line.startswith(("*", "-"))
                     for name in line.strip().split(", ")
                     if name and name not in non_names)
无论是哪种方式,结果都是
{'John Smith'、'Jeannie Jones'、'Mike Smith'、'Bob Smith'、'McAleer Chung'、'Steve Carlos'、'Brown Williams'、'Jessica Yi'、'Paul Garcia'、'Jane Jones'、'Shu K Smith'、'Song Kim'、'Daniel le。要获得已排序的名称,只需对
集合进行排序
,或者如果要按姓氏排序,请使用特殊的
功能:

names = sorted(names, key=lambda s: s.split()[-1])
潜在解决方案:

假定文件的格式与您给定的相同 逐行检查文件 忽略第一个和第二个单词未大写的所有行 然后将该行作为名称列表处理

for line in file:
  words = line.split(",")

  #No one has just one name like Tupac
  if len(words) > 1:
    #Check to see if first letter of both words are uppercase
    if isUpper(words[0][0]) and isUpper(words[1][0]):
      #name line
      list_to_be_returned+=words
类似的东西可能是潜在的解决方案:

with open(filename)as file:
    words = file.read().split()
假定文件的格式与您给定的相同 逐行检查文件 忽略第一个和第二个单词未大写的所有行 然后将该行作为名称列表处理

for line in file:
  words = line.split(",")

  #No one has just one name like Tupac
  if len(words) > 1:
    #Check to see if first letter of both words are uppercase
    if isUpper(words[0][0]) and isUpper(words[1][0]):
      #name line
      list_to_be_returned+=words
大概是这样吧

with open(filename)as file:
    words = file.read().split()
也可以使用正则表达式

import re

with open(filename)as file:
    words = re.findall(r'([\w]+)', file.read())
也可以使用正则表达式

import re

with open(filename)as file:
    words = re.findall(r'([\w]+)', file.read())

不太清楚。您能为该文件显示一些示例输出吗?另外,我认为仅仅是连接所有行并删除
\n
会让事情变得更困难。@tobias_k我添加了我想要的输出!我不太清楚。您能为该文件显示一些示例输出吗?另外,我认为仅仅是连接所有行并删除
\n
会让事情变得更困难。@tobias_k我添加了我想要的输出!谢谢你,我能清楚地理解你的解释。如果字符串类似于“non name name”,即“school James Harden”,则school与虚线和James之间没有空格。我怎样才能从中得到詹姆斯·哈登?我想当你使用line.strip().split(“,”@YunTaeHwang时,我可以再添加一个delimeter。你可能也可以用
-
进行拆分,但是包含
-
的名称呢?这些情况并不少见。最好使用一些正则表达式(假设它们只能出现在行的开头)从行的开头剥离它们。您可以在re.sub(“^[a-z]+-”,line.strip()).split(“,”)中尝试
查找名称。谢谢,我可以清楚地理解您的解释。如果字符串类似于“non name name”,即“school James Harden”,则school与虚线和James之间没有空格。我怎样才能从中得到詹姆斯·哈登?我想当你使用line.strip().split(“,”@YunTaeHwang时,我可以再添加一个delimeter。你可能也可以用
-
进行拆分,但是包含
-
的名称呢?这些情况并不少见。最好使用一些正则表达式(假设它们只能出现在行的开头)从行的开头剥离它们。您可以在re.sub(“^[a-z]+-”,line.strip())中尝试
查找名称。拆分(“,”)
这不是不完整吗?你只需要得到一个单词列表;你既不过滤掉没有名字的部分,也不知道哪些单词是同一个名字的一部分。这不是不完整吗?你只需要得到一个单词列表;您既不过滤掉非名称部分,也不知道哪些单词是同一名称的一部分。