I don't know what is causing this error...please help

Thread Solved

Join Date: Nov 2009
Posts: 61
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training

I don't know what is causing this error...please help

 
0
  #1
21 Days Ago
Hi

I am ery new to Python. I've only started using it yesterday. Please keep that in mind, as the error might seem simple to you, but I can't understand it.

I am trying to run the following piece of code:
  1. import re
  2.  
  3. foo = open("PREFIX.txt", "r+")
  4. text = foo.read # Read in from file
  5. foo.close()
  6.  
  7. special = re.compile(r"[etn]") # Not sure what this does.
  8.  
  9. def escape(match): # Specifies that any \ has to be escaped
  10. return "\\" + match.group(0)
  11.  
  12. if __name__ == "__main__":
  13. out = open("WRITE.txt", "w")
  14. out.write(special.sub(escape, text)) # Write to file
  15. out.close()

When I check the code using Python's built-in checker, everyhitng checks out fine. However, when I use the console to run the code, the following text is all that appears(It doesnt write to the file):

Traceback (most recent call last):
File "advanced.py", line 14, in (module)
out.write(special.sub(escape, text))
TypeError: expected string of buffer

I don't know if this is vital information: the file I am reading from contains XML, but it is in a text file, so I assumed it would handle everything as text. The goal of the code is to escape the backslashes in the XML.

Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC