我有一个名称文件,格式如下:
Last, First Middle Initial
在某些情况下,个体可能没有中间首字母
Last, First
我想使用正则表达式将顺序更改为
First Middle, Last name
或
First, Last Name (when there is no middle initial)
有没有人能帮我写出能完成这个任务的表达式?
如何使用regex为这些情况设置条件字段?我想设置一个if函数,如果name字段格式等于这两种可能性中的一种,它将使用相应的表达式来重新排列名称。我正在使用Spotfire客户端来执行此操作。
谢谢!
发布于 2015-07-21 15:00:02
实施:
import re
R = re.compile(r"(\w+),\s+(\w+)(\s*\w*)")
def convert(s):
return R.sub(r"\2\3, \1", s)
和测试:
assert convert("Last, First Middle") == "First Middle, Last"
assert convert("Last, First") == "First, Last"
https://stackoverflow.com/questions/31542236
复制