| | |
TypeError: 'list' object is not callable
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
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
"
I just want to know why this error is occurring and how it can be remedied. Any help would be great, thanks.
"
csv_data = csv.reader(file('TxtFile1.txt', 'r'))
TypeError: 'list' object is not callable
"
Python Syntax (Toggle Plain Text)
import MySQLdb, csv, sys conn = MySQLdb.connect (host = "localhost",user = "a", passwd = "b",db = "c") c = conn.cursor() csv_data = csv.reader(file('TxtFile1.txt', 'r')) for row in csv_data: print row row = csv_data.next() c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row) #c.commit() #c.close()
I just want to know why this error is occurring and how it can be remedied. Any help would be great, thanks.
•
•
Join Date: Sep 2009
Posts: 100
Reputation:
Solved Threads: 11
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:
I believe this should work, if not post back and we can try something else.
Python Syntax (Toggle Plain Text)
import MySQLdb, csv, sys conn = MySQLdb.connect (host = "localhost",user = "a", passwd = "b",db = "c") c = conn.cursor() csv_data = csv.reader(file('TxtFile1.txt')) for row in csv_data: print row row = csv_data.next() c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row) #c.commit() #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.
•
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
0
#3 Oct 20th, 2009
•
•
•
•
csv_data = csv.reader(open('TxtFile1.txt'))
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']
0
#4 Oct 20th, 2009
You should uncomment your
commit statement. You need to commit your transactions before they go through. •
•
Join Date: Jun 2008
Posts: 5
Reputation:
Solved Threads: 0
0
#5 Oct 20th, 2009
•
•
•
•
You should uncomment your commit statement. You need to commit your transactions before they go through. This is the current code:
Python Syntax (Toggle Plain Text)
import MySQLdb, csv, sys db = MySQLdb.connect(user="a", passwd="b", db = "c") c = db.cursor() # This statement works fine #c.execute ("INSERT INTO popularity (Category, Value) VALUES ('alpha', 'beta')") csv_data = csv.reader(open('TxtFile1.txt', 'r')) for row in csv_data: print row c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", row)
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.
1
#6 Oct 20th, 2009
•
•
•
•
The print row command outputs:
['test one']
I want 'test' to go into Category and 'one' to go into Value.
python Syntax (Toggle Plain Text)
c.execute("INSERT INTO a (Category, Value) VALUES (%s, %s)", tuple(row[0].split()))
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)
>>> print 'Hey %s totally %s check' % ('test', 'one') Hey test totally one check >>>
%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)
>>> row = ['test one'] >>> row[0] 'test one' >>> row[0].split() ['test', 'one'] >>> tuple(row[0].split()) ('test', 'one') >>>
Last edited by jlm699; Oct 20th, 2009 at 11:25 pm.
![]() |
Similar Threads
- 'file' object is not callable (Python)
- Help with fixing "str object is not callable" error (Python)
- Annoying Student Plea for Help (Python)
- Urgent!!!!!!URGENTTTTT! (Python)
- os.execv() (Python)
- here it is (Python)
- Multiple argument passing (Python)
- adding a bunch of List object to a set, and a bigger problem (Java)
- Dictionary of Functions (Python)
Other Threads in the Python Forum
- Previous Thread: Decimal to Binary Conversion (Python)
- Next Thread: form have 2 iframe.problem to input text in other iframe
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






