I have been trying to search for this answer on online tutorials, but cannot figure out just what I am doing wrong.
I have a list of names, and some of them have the character '/' For example:
list = [adam, bill, jon/juan]
Because later in my code, I use each name to start a file, this '/' is problematic because it tries to open a new directory. I want the program to search the entries for this character, and if found, delete it. I am trying to use regular expressions:
import re slash = re.compile("/") def get_names(file=None): infile = open(file) Name_List= for line in infile: line = line.strip() sline = line.split() if sline not in Name_List: if not re.match(slash, line): #NOT WORKING! Name_List.append(sline) return Name_List
I've tried replacing "if not re.match(slash, line)" with "if not re.match(slash, sline)" but it is not helping. The code is making new files until it hits one of these problematic list entries, then it just crashes. Any advice?