0

I'm trying to write some text to a file in python, however I'm running into a problem with the following line

<ClCompile Include="..\src\node\Node.cpp" />

It seems that python is interpreting the \n in \node as a new line. I need to solve this problem while still maintaining the actual new lines in the file.

EDIT

Upon further investigation it seems that this is being caused by a regex substitution call that I'm making.

A summarized version is a follows:

mainString = mainFile.read() 
compileRegex = re.compile(ur'(<ClCompile(.*/>\n)*)')
compileMatch = compileRegex.search(mainString)
compileString = compileMatch.group(1) 
tempFileString = tempFile.read()
tempFileString = re.sub(compileRegex, compileString, tempFileString)
rykeeboy
  • 645
  • 2
  • 8
  • 22

1 Answers1

0

Use raw strings

In [1]: r".\n."
Out[1]: '.\\n.'
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120
  • The problem is that I'm reading the string from a file and performing regex operations on it. Is there a way to get this to work as a raw string. I've tried escaping the back slashes with double backslashes but it doesn't seem to make a difference. My code is as follows https://gist.github.com/RyanBluth/ac25372b38bb761edf2a – rykeeboy Mar 13 '15 at 15:44
  • A read file is already literal. Don't worry about it. – Malik Brahimi Mar 13 '15 at 15:46
  • I saw that when doing some research as well, however it doesn't solve my issue, meaning something else is going on. Perhaps its a problem with the regex substitution that I'm doing? – rykeeboy Mar 13 '15 at 15:48