Reputation: 1072
I'm encountering the SyntaxError: EOL while scanning string literal
error when running timeit.Timer
. My setup is a long string spanning multiple lines:
setup = """
import datetime
import glob
import os
import pandas as pd
ydate = str(20180308)
# Import
path = 'path_to_file'
all_files = glob.glob(os.path.join(path, '*.csv'))
all_files = [x for x in all_files if ydate in x]
all_files = [x.replace('\\','/') for x in all_files]"""
I've moved quotation marks around, tried swapping around the '
and "
locations, anything I could think of that usually fixes this error but it still throws the error when I run
t = timeit.Timer("[pd.read_csv(x,encoding='latin-1') for x in all_files]", setup=setup)
t.timeit()
Also open to a different way to time this. I'm basically comparing the time for map
vs a list comprehension solution.
Upvotes: 0
Views: 197
Reputation: 15433
The SyntaxError occurred in the last line of the setup. One solution is to add a pair of backslashes on the last line:
setup = """
import datetime
import glob
import os
import pandas as pd
ydate = str(20180308)
# Import
path = 'path_to_file'
all_files = glob.glob(os.path.join(path, '*.csv'))
all_files = [x for x in all_files if ydate in x]
all_files = [x.replace('\\\\', '/') for x in all_files]"""
However, glob
should return valid paths for your operating system. So if you replace the backslash by the forward slash, your OS might not be able to find the files anymore. In that case, I would recommend skipping the last line of your setup:
setup = """
import datetime
import glob
import os
import pandas as pd
ydate = str(20180308)
# Import
path = 'path_to_file'
all_files = glob.glob(os.path.join(path, '*.csv'))
all_files = [x for x in all_files if ydate in x]"""
Upvotes: 1