944,126 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 7146
  • Python RSS
Oct 20th, 2009
0

TypeError: 'list' object is not callable

Expand Post »
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
"
Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shadow-Illusion is offline Offline
5 posts
since Jun 2008
Oct 20th, 2009
0
Re: TypeError: 'list' object is not callable
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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009
Oct 20th, 2009
0
Re: TypeError: 'list' object is not callable
Quote ...
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']
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shadow-Illusion is offline Offline
5 posts
since Jun 2008
Oct 20th, 2009
0
Re: TypeError: 'list' object is not callable
You should uncomment your commit statement. You need to commit your transactions before they go through.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 20th, 2009
0
Re: TypeError: 'list' object is not callable
Click to Expand / Collapse  Quote originally posted by jlm699 ...
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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shadow-Illusion is offline Offline
5 posts
since Jun 2008
Oct 20th, 2009
1
Re: TypeError: 'list' object is not callable
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:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 21st, 2009
0
Re: TypeError: 'list' object is not callable
Thanks jlm, that worked perfectly - everything is great now.
I just needed the spliting code at the end.

Thanks again ~ solved.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Shadow-Illusion is offline Offline
5 posts
since Jun 2008
Oct 14th, 2010
0
Re: TypeError: 'list' object is not callable
Hi i am trying to count column k in the csv file and increment it if the next values is the same as the first value
for example,


p(ki) k sum(k)
0.117909 106 1
0.067853 61 1
0.048943 44 1
0.047831 43 3
0.047831 43
0.047831 43
0.046719 42 2
0.046719 42





Python Syntax (Toggle Plain Text)
  1.  
  2. import csv
  3.  
  4.  
  5. #choosing the csv file to be imported
  6. reader = csv.reader(open("NoOfCounts1.csv","r"))
  7.  
  8. #this will print the second cloumn in the dataset
  9.  
  10. nobracket = []
  11. NoOfCounts = []
  12. total = 0
  13. for urls,NoOfCounts in reader:
  14. #This is getting rid of the commas behind the numbers
  15. if NoOfCounts[-1] == ')':
  16. s2 = NoOfCounts[:-1]
  17. s3 = int(s2)
  18. nobracket.append(s3)
  19. NoOfCounts = NoOfCounts[:-1]
  20. total +=int(NoOfCounts[1])
  21.  
  22. print 'The total is =' ,total # the result
  23.  
  24. sortedk = sorted(nobracket)
  25.  
  26. # A new list to store ki(s)
  27. kilist = []
  28. sumk = 0
  29. prevk = thisk
  30. counts =[(thisk,1)]
  31. for thisk in sortedk:
  32. count = counts[-1][1]
  33. sumk += 1
  34. counts = [-1] (thisk,count)
  35. else:
  36. print 'sum of k:',counts[-1]
  37. counts.append((thisk,1))
  38. prevk = thisk
  39.  
  40.  
  41.  
  42. for ki in nobracket:
  43. pki = ki/float(total) #converts the total to a floating number
  44. print pki,ki
  45. kilist.sort()
  46. kilist.append(kilist)
  47. print( '%f \t %d' % (pki,ki))
  48.  
  49.  
  50. print ('%i \t %i')%(k,sum)



this is the error i am getting at a moment


Traceback (most recent call last):
File "C:\Python26\First Experiment\pk2.py", line 34, in <module>
counts = [-1](thisk,count)
TypeError: 'list' object is not callable
>>>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djmagba is offline Offline
1 posts
since Oct 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to change text file encoding with python?
Next Thread in Python Forum Timeline: String Letter-Counter Function Problems - Help Urgent!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC