I've been trying to parse a text file and manipulate it with regular expressions. This is my script:
import re
original_file = open('jokes.txt', 'r+')
original_file.read()
original_file = re.sub("\d+\. ", "", original_file)
How to fix the following error:
Traceback (most recent call last):
File "filedisplay.py", line 4, in <module>
original_file = re.sub("\d+\. ", "", original_file)
File "C:\Python32\lib\re.py", line 167, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer
And why am I getting this error?
original_fileis a file object, you need to read it to get its contents, or the buffer that the regex requires.original_file, so you're still using a file object in the regex. Why don't you use another variable? Likecontents = original_file.read()?