1

Beginner here. I have the following circumstances.

  • A text file with each line containing a name.
  • A cassandra 3.5 database
  • A python script

The intention is to have the script read from the file one line (one name) at a time, and query Cassandra with that name.

FYI, everything works fine except for when I try to pass the value of the list to the query.

I current have something like:

#... 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)

Everything works fine if I remove the tagname list component and edit the query itself with a name value.

What am I doing wrong here?

3
  • What excpetion are you getting? NameError: name 'tagname' is not defined ? Commented Sep 13, 2017 at 15:36
  • Also what is the expected value of tagname = str(content[x])? It might be None, and the root cause. Commented Sep 13, 2017 at 15:41
  • @Vinny Unfortunately, I'm not getting any error. The print row.name.... simply isn't printing. No errors at all. And now, tagname = str(content[x]) is not none. print tagname works perfectly. Commented Sep 13, 2017 at 16:03

2 Answers 2

1

Simply building on the answer from @Vinny above, format simply replaces literal value. You need to put quotes around it.

for x in content:
    rows = session.execute("SELECT * FROM tablename where name ='{}' and date ='{}' order by time desc limit 1".format(x, startDay))
    for row in rows:
        print row.name + ", " + str(row.date)
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply iterate over content:

for x in content:

    rows = session.execute("SELECT * FROM tablename where name = {} and date = {} order by time desc limit 1".format(x, startDay))
    for row in rows:
        print row.name + ", " + str(row.date)
    ....

Also, you don't need to have 3 quotes for the string. Single quotes is good enough (3 quotes is used for documentation / multiple line comments in python)

Note that this might end in a different error; but you will be iterating on the lines instead of iterating over an index and reading lines.

4 Comments

Thanks. Three quotes, multiple lines. Got it. I modified the code. It helped me avoid having to read the number of lines. In the query, I changed %s to {} as per your snippet. I get the following error now: TypeError: not all arguments converted during string formatting
@bshakya it might be you have a None object. Use debug prints to check the type of tag name print type(x) before your original print
An observation. print x apparently includes a \n at the end of it. So when i print it, i can see a new line being printed as well. I think that might be the problem? How do I remove that and transform the value into a pure string?
And the output of print type(x) is: <type 'str'>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.