0

I am using following code :

re.sub(inputpattern,outputpattern, currentline)

in the above code , i am reading the value of outputpattern from csv , whose value is :

\\1-\\2-\\3-\\4

I am reading it like below :

outputpattern = row['PREFIX_1_WRT_FMT']

I have also tried :

outputpattern = "'"+ row['PREFIX_1_WRT_FMT'] +"'"

The problem is that it is not treating it as proper format , but if I hard code it like below it works fine :

re.sub(inputpattern,'\\1-\\2-\\3-\\4', currentline)

1
  • How does the csv file get written? It looks like outputpattern has double backslashes in it. What do you see when you open the csv file in a text editor?
    – ekhumoro
    Commented Dec 1, 2014 at 22:53

1 Answer 1

1

You only need to escape the backslashes if you have it as a literal string.

"\\1-\\2-\\3-\\4"

If you read it from an input you don't have to do that. You need to change the pattern inside the CSV to be like \1-\2-\3-\4

You could also use the raw string if you dislike escaping every char when using literal string, by prefixing you string with the letter r.

r"\1-\2-\3-\4"
1
  • @IshuGupta: If Meitham's answer solved your problem, please mark it as Accepted (the big check-mark). Commented Dec 2, 2014 at 1:03

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.