1
engine = create_engine("")
df = pd.read_csv('in.csv', chunksize=1000) 
for chunk in df: 
    list= tuple(list(chunk["column2"])) 
    sql = "SELECT * from table where value in {};".format(list) 
    found = pd.read_sql(sql, engine) 
    found.to_csv('out.csv', mode='a', header ['column2'], index=False)

an error appeared and I'm not sure why and how to fix:

list= tuple(list(chunk["column2"]))
TypeError: 'tuple' object is not callable
3
  • 1
    Please do not name variables list. It overrides a builtin python name and will lead to hard to find errors. Commented Mar 27, 2018 at 14:52
  • 3
    On the first iteration tuple(list(chunk["column2"])) works and assigns its result to the name list. On the second iteration list is now a tuple, so the inner part list(chunk["column2"]) is calling a tuple, not the builtin list. Commented Mar 27, 2018 at 14:53
  • Possible duplicate of TypeError: 'list' object is not callable in python Commented Mar 27, 2018 at 15:03

2 Answers 2

1

Your issue is that you are overwriting what list is. You are assigning it to a tuple, and then calling it again, but instead of calling Python's builtin list, you are trying to call a tuple:

list = tuple(list((1,2)))   # this will work since you haven't reassigned list yet
list = tuple(list((2,3)))   # this will throw an error.
Sign up to request clarification or add additional context in comments.

Comments

0

There is a better approach - save your DF as a temporary (additional) table in your DB and use it for the subquery:

df[['column2']].to_sql('tmp', engine, if_exists='replace')
sql = """
SELECT * from table
where value in (select column2 from tmp)
"""
found = pd.read_sql(sql, engine)

or save it directly to CSV:

(pd.read_sql(sql, engine)
   .to_csv('out.csv', mode='a', header=['column2'], index=False))

Comments

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.