【发布时间】:2018-02-22 09:34:03
【问题描述】:
这里是初学者。 我有以下情况。
- 每行包含一个名称的文本文件。
- cassandra 3.5 数据库
- python 脚本
目的是让脚本一次从文件中读取一行(一个名称),并使用该名称查询 Cassandra。
仅供参考,除了我尝试将列表的值传递给查询时,一切正常。
我目前有类似的东西:
#... driver import, datetime imports done above
#...
with open(fname) as f:
content = f.readlines()
# Loop for each line from the number of lines in the name list file
# num_of_lines is already set
for x in range(num_of_lines):
tagname = str(content[x])
rows = session.execute("""SELECT * FROM tablename where name = %s and date = %s order by time desc limit 1""", (tagname, startDay))
for row in rows:
print row.name + ", " + str(row.date)
如果我删除 tagname 列表组件并使用名称值编辑查询本身,一切正常。
我在这里做错了什么?
【问题讨论】:
-
你得到了什么异常?
NameError: name 'tagname' is not defined? -
还有
tagname = str(content[x])的期望值是多少?这可能是无,而且是根本原因。 -
@Vinny 不幸的是,我没有收到任何错误。打印 row.name.... 根本没有打印。完全没有错误。现在,tagname = str(content[x]) 不是没有。打印标记名完美运行。
标签: python python-2.7 cassandra