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
"

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.

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:

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.

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:


You should uncomment your commit statement. You need to commit your transactions before they go through.

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:

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)

I think I need to replace '%s' with something, but I'm not sure on what exactly.

The print row command outputs:


I want 'test' to go into Category and 'one' to go into Value.

Edited 7 Years Ago by Shadow-Illusion: n/a

The print row command outputs:


I want 'test' to go into Category and 'one' to go into Value.

In that case you'll want this:

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 . 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:

>>> print 'Hey %s totally %s check' % ('test', 'one')
Hey test totally one check
>>>

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:

>>> row = ['test one']
>>> row[0]
'test one'
>>> row[0].split()
['test', 'one']
>>> tuple(row[0].split())
('test', 'one')
>>>

Edited 7 Years Ago by jlm699: n/a

Comments
Solved problem well

Thanks jlm, that worked perfectly - everything is great now.
I just needed the spliting code at the end.

Thanks again ~ solved.

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

import csv


#choosing the csv file to be imported
reader = csv.reader(open("NoOfCounts1.csv","r"))

#this will print the second cloumn in the dataset

nobracket = []
NoOfCounts = []
total = 0 
for urls,NoOfCounts in reader:
    #This is getting rid of the commas behind the numbers
    if NoOfCounts[-1] == ')':
           s2 =  NoOfCounts[:-1]
           s3 = int(s2)
           nobracket.append(s3)
           NoOfCounts = NoOfCounts[:-1]
           total  +=int(NoOfCounts[1])

print 'The total is =' ,total # the result

sortedk = sorted(nobracket)

# A new list to store  ki(s)
kilist = []
sumk = 0
prevk = thisk
counts =[(thisk,1)]
for thisk in sortedk:
    count = counts[-1][1]
    sumk += 1
    counts = [-1] (thisk,count)
else:
        print 'sum of k:',counts[-1]
        counts.append((thisk,1))
        prevk = thisk
    
    
    
for ki in nobracket:
    pki = ki/float(total) #converts the total to a floating number
   print pki,ki
   kilist.sort()
   kilist.append(kilist)
   print( '%f \t %d' %  (pki,ki))


   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
>>>

This question has already been answered. Start a new discussion instead.