timeit 设置语句中的 EOL 错误
EOL error in timeit setup statement
我在 运行 宁 timeit.Timer
时遇到 SyntaxError: EOL while scanning string literal
错误。我的设置是一个跨越多行的长字符串:
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]"""
我已经移动了引号,尝试交换 '
和 "
位置,任何我能想到的通常都会修复这个错误,但是当我 运行
t = timeit.Timer("[pd.read_csv(x,encoding='latin-1') for x in all_files]", setup=setup)
t.timeit()
也可以用不同的方式来计时。我基本上是在比较 map
与列表理解解决方案的时间。
设置的最后一行发生语法错误。一种解决方案是在最后一行添加一对反斜杠:
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]"""
但是,glob
应该 return 适用于您的操作系统的有效路径。因此,如果您将反斜杠替换为正斜杠,您的 OS 可能无法再找到这些文件。在这种情况下,我建议您跳过设置的最后一行:
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]"""
我在 运行 宁 timeit.Timer
时遇到 SyntaxError: EOL while scanning string literal
错误。我的设置是一个跨越多行的长字符串:
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]"""
我已经移动了引号,尝试交换 '
和 "
位置,任何我能想到的通常都会修复这个错误,但是当我 运行
t = timeit.Timer("[pd.read_csv(x,encoding='latin-1') for x in all_files]", setup=setup)
t.timeit()
也可以用不同的方式来计时。我基本上是在比较 map
与列表理解解决方案的时间。
设置的最后一行发生语法错误。一种解决方案是在最后一行添加一对反斜杠:
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]"""
但是,glob
应该 return 适用于您的操作系统的有效路径。因此,如果您将反斜杠替换为正斜杠,您的 OS 可能无法再找到这些文件。在这种情况下,我建议您跳过设置的最后一行:
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]"""