简体   繁体   中英

creating a regular expression in python with variables and a comma

I am trying to match and find names exactly from one list with another list using python.

# First List
file = 'Last_First.csv'
filename = file.split('_')
last = filename[0]
first = filename[1]

search a large list of names where names are saved as Last, First

pattern = re.compile(re.escape(last+','+first))
# Second List
['63', 'Last, First', '65164345']

when i search the list line by line, i get an empty list

matches = pattern.findall(line)

printing the pattern , i get

re.compile(r'Last\,First', re.UNICODE)

how can i get rid of \\ ?

The \\ is an escape character that has to be there. You are getting an empty list because 'Last, First' has a space after the comma, your regular expression is not matching that space.

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