Jupyter Notebook 中的循环和字符串格式化
For loop and string formating in Jupyter Notebook
我定义了以下元组:
stocks = ('san', 'bbva', 'ele')
我使用 for 循环 运行 查询每只股票:
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
问题是,当我在终端上 运行 时,查询完美地存储在 queryTp
:
ipdb> queryTp
"\tSELECT\tdate,close \n \t\t\t\tFROM san_prices \n \t\t\t\tWHERE date >=
'2000-01-03 00:00:00' "
然而,当运行在 Jupyter Notebook 上使用它时,查询存储为
"\tSELECT\tdate,close FROM s_prices WHERE date >= '2000-01-03 00:00:00' "
一开始我以为是跟字符串格式有关。不过,datetime(2000,1,3)
介绍的很好。然后我意识到变量 i
不是遍历 stocks
值(即 'san', 'bbva', 'ele'
)而是遍历变量名。换句话说,在执行 print(i)
时,我获得了值 's':
print(i)
's'
stocks = ('san', 'bbva', 'ele')
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
适合我。结果:
SELECT date,close
FROM san _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM bbva _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM ele _prices
WHERE date >= '2000-01-03 00:00:00'
您的终端和笔记本上 运行 的 python 是什么版本?
编辑:
您确定要使用全部 3 只股票进行测试吗?如果您使用以下代码仅在一个上进行测试:
stocks = ('san')
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
它会影响 return:
SELECT date,close
FROM s _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM a _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM n _prices
WHERE date >= '2000-01-03 00:00:00'
这是有道理的,因为 'san' 被视为可迭代的,因为字符串在每个字符上都是可迭代的。 (例如 [x for x in 'abc'] returns ['a', 'b', 'c']。)如果你只想测试你的代码一只股票你应该像这样创建循环:
stocks = ('san',)
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
其中 return 是一条语句:
SELECT date,close
FROM san _prices
WHERE date >= '2000-01-03 00:00:00'
我定义了以下元组:
stocks = ('san', 'bbva', 'ele')
我使用 for 循环 运行 查询每只股票:
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
问题是,当我在终端上 运行 时,查询完美地存储在 queryTp
:
ipdb> queryTp
"\tSELECT\tdate,close \n \t\t\t\tFROM san_prices \n \t\t\t\tWHERE date >=
'2000-01-03 00:00:00' "
然而,当运行在 Jupyter Notebook 上使用它时,查询存储为
"\tSELECT\tdate,close FROM s_prices WHERE date >= '2000-01-03 00:00:00' "
一开始我以为是跟字符串格式有关。不过,datetime(2000,1,3)
介绍的很好。然后我意识到变量 i
不是遍历 stocks
值(即 'san', 'bbva', 'ele'
)而是遍历变量名。换句话说,在执行 print(i)
时,我获得了值 's':
print(i)
's'
stocks = ('san', 'bbva', 'ele')
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
适合我。结果:
SELECT date,close
FROM san _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM bbva _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM ele _prices
WHERE date >= '2000-01-03 00:00:00'
您的终端和笔记本上 运行 的 python 是什么版本?
编辑: 您确定要使用全部 3 只股票进行测试吗?如果您使用以下代码仅在一个上进行测试:
stocks = ('san')
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
它会影响 return:
SELECT date,close
FROM s _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM a _prices
WHERE date >= '2000-01-03 00:00:00'
SELECT date,close
FROM n _prices
WHERE date >= '2000-01-03 00:00:00'
这是有道理的,因为 'san' 被视为可迭代的,因为字符串在每个字符上都是可迭代的。 (例如 [x for x in 'abc'] returns ['a', 'b', 'c']。)如果你只想测试你的代码一只股票你应该像这样创建循环:
stocks = ('san',)
for i in stocks:
queryTp = """SELECT date,close
FROM %s _prices
WHERE date >= '%s' """ % (i, datetime(2000,1,3))
print(queryTp)
其中 return 是一条语句:
SELECT date,close
FROM san _prices
WHERE date >= '2000-01-03 00:00:00'