I'm attempting to convert a column of a dataframe from a string to a date, however the string contains "\". When scraping the data, it looks like so:
movie_data = pd.read_html("http://www.boxofficemojo.com/movies/?page=daily&view=chart&id=everest2015.htm", 'r')
date = movie_data[6][1][2:]
print(date.head())
2 Sep\t. 18, 2015
3 Sep\t. 19, 2015
4 Sep\t. 20, 2015
5 Sep\t. 21, 2015
I want to iterate over this column to convert this string into a date, however I can't escape the syntax error I'm getting when trying to execute the following:
for d in date:
date[d] = date[d].replace(".","")
date[d] = date[d].replace("\","")
date[d] = datetime.strptime(date[d], %M %D, %Y)
print(date)
Error:
File "<ipython-input-51-eb78788bf83f>", line 14
date[d] = date[d].replace("\","")
^
SyntaxError: EOL while scanning string literal
I realize that the character I'm using is used to escape quotations, but I have no idea how to use this character when referencing replace.
Any help would be greatly appreciated. Thanks in advance.
It is because you are using escape character, so python assumes next "
to be escaped, thereby causing unbalanced quotes and SyntaxError
.
Escaping the character would fix the problem:-
date[d] = date[d].replace("\\","") # line no 14