TypeError: 'list' object is not callable

Thread Solved

Join Date: Jun 2008
Posts: 5
Reputation: Shadow-Illusion is an unknown quantity at this point 
Solved Threads: 0
Shadow-Illusion Shadow-Illusion is offline Offline
Newbie Poster

TypeError: 'list' object is not callable

 
0
  #1
Oct 20th, 2009
I am trying to insert data from a text file into an SQL table. I'm getting this error:

"
csv_data = csv.reader(file('TxtFile1.txt', 'r'))
TypeError: 'list' object is not callable
"
  1. import MySQLdb, csv, sys
  2. conn = MySQLdb.connect (host = "localhost",user =
  3. "a", passwd = "b",db = "c")
  4. c = conn.cursor()
  5. csv_data = csv.reader(file('TxtFile1.txt', 'r'))
  6. for row in csv_data: print row
  7. row = csv_data.next()
  8. c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row)
  9. #c.commit()
  10. #c.close()

I just want to know why this error is occurring and how it can be remedied. Any help would be great, thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 100
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 11
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
0
  #2
Oct 20th, 2009
Well it looks like you may be opening the file wrong, i don't know why is raising that error, you're not even using a list object. Try this:
  1. import MySQLdb, csv, sys
  2. conn = MySQLdb.connect (host = "localhost",user =
  3. "a", passwd = "b",db = "c")
  4. c = conn.cursor()
  5. csv_data = csv.reader(file('TxtFile1.txt'))
  6. for row in csv_data: print row
  7. row = csv_data.next()
  8. c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row)
  9. #c.commit()
  10. #c.close()

I believe this should work, if not post back and we can try something else.
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: Shadow-Illusion is an unknown quantity at this point 
Solved Threads: 0
Shadow-Illusion Shadow-Illusion is offline Offline
Newbie Poster
 
0
  #3
Oct 20th, 2009
csv_data = csv.reader(open('TxtFile1.txt'))
This seems to have fixed the problem.

However im trying to input the rows from the textfile into a SQL table. The 'insert' statement isnt actually doing anything, any ideas to fix it?

The output of the textfile came out like:
['test one']
['test two']
['test three']
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #4
Oct 20th, 2009
You should uncomment your commit statement. You need to commit your transactions before they go through.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: Shadow-Illusion is an unknown quantity at this point 
Solved Threads: 0
Shadow-Illusion Shadow-Illusion is offline Offline
Newbie Poster
 
0
  #5
Oct 20th, 2009
Originally Posted by jlm699 View Post
You should uncomment your commit statement. You need to commit your transactions before they go through.
Yeah I'm aware of that, I uncomment it and try and commit to the DB when the code isn't showing errors.
This is the current code:
  1. import MySQLdb, csv, sys
  2. db = MySQLdb.connect(user="a", passwd="b", db = "c")
  3. c = db.cursor()
  4. # This statement works fine #c.execute ("INSERT INTO popularity (Category, Value) VALUES ('alpha', 'beta')")
  5. csv_data = csv.reader(open('TxtFile1.txt', 'r'))
  6. for row in csv_data:
  7. print row
  8. c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row)
I think I need to replace '%s' with something, but I'm not sure on what exactly.

The print row command outputs:
['test one']

I want 'test' to go into Category and 'one' to go into Value.
Last edited by Shadow-Illusion; Oct 20th, 2009 at 10:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #6
Oct 20th, 2009
Originally Posted by Shadow-Illusion View Post
The print row command outputs:
['test one']

I want 'test' to go into Category and 'one' to go into Value.
In that case you'll want this:
  1. c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", tuple(row[0].split()))
Basically this takes the element [0], which represents that string 'test one', and then splits it (the default split delimiter is a space), so it turns it into ['test', 'one']. Finally, you just need to turn it into a tuple.

When doing string formatting, you need to have a tuple containing each argument for string formatting as an element. Here's an example of a string formatting requiring two arguments:
  1. >>> print 'Hey %s totally %s check' % ('test', 'one')
  2. Hey test totally one check
  3. >>>
As you can see, I need two things to fill into the %s pieces. The two elements in the tuple satisfy this requirement.

Here's the transformation from "row" to the tuple required in broken down steps:
  1. >>> row = ['test one']
  2. >>> row[0]
  3. 'test one'
  4. >>> row[0].split()
  5. ['test', 'one']
  6. >>> tuple(row[0].split())
  7. ('test', 'one')
  8. >>>
Last edited by jlm699; Oct 20th, 2009 at 11:25 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 5
Reputation: Shadow-Illusion is an unknown quantity at this point 
Solved Threads: 0
Shadow-Illusion Shadow-Illusion is offline Offline
Newbie Poster
 
0
  #7
Oct 21st, 2009
Thanks jlm, that worked perfectly - everything is great now.
I just needed the spliting code at the end.

Thanks again ~ solved.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC