0
from cs50 import SQL, get_string
from sys import argv
import csv
db = SQL("sqlite:///students.db")

if (len(argv) != 2):
    exit()
with open(argv[1],"r") as inp:
    reader = csv.DictReader(inp)
    for row in reader:
        lis = row["name"].split
        if (len(lis) == 2):
            db.execute("INSERT INTO students (name, house, birth) VALUES (list[0], NULL, list[1]")
        else:
            db.execute("INSERT INTO students (name, house, birth) VALUES (list[0], list[1], list[2]")

I wish to store the split strings in a list, but I get this error. TypeError: object of type 'builtin_function_or_method' has no len()

1

Assume error comes at if (len(lis) == 2): (there are two "len" in the program).

The problem is here lis = row["name"].split, because it is not calling the split function. It is declaring lis a function (use print(lis()) to see it in action).

The correct syntax would be lis = row["name"].split().

Spoiler alert: program names the array lis, but is using list[i] in INSERT.

| improve this answer | |
  • Oh thanks a lot! – Varun Beluguli Mar 24 at 13:59
  • reader = csv.DictReader(inp) for row in reader: lis = row["name"].split() error: File"import.py",line 11, in <module> for row in reader: File "/usr/local/lib/python3.7/csv.py" line 111 in next self.fieldnames File "/usr/local/lib/python3.7/csv.py", line 98, in fieldnames self._fieldnames = next(self.reader) File "/usr/local/lib/python3.7/codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xda in position 106: invalid continuation byte – Varun Beluguli Mar 24 at 15:54
  • best guess python import.py students.db at the command line. students.db is not a csv file. – DinoCoderSaurus Mar 24 at 16:08
  • Oh my gawd! Thanks! Thats was so silly – Varun Beluguli Mar 24 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .