2

I'm trying to put the data file of schools.data which is just a file listing many universities. It says 'type' object is not subscriptable in terminal. Here is the code

import urllib
import sqlite3
import json
import time
import ssl

conn = sqlite3.connect('universityrawdata.sqlite')
cur = conn.cursor()

cur.execute('''CREATE TABLE IF NOT EXISTS Universitylocations (address TEXT, geodata TEXT)''')
fh = open("schools.data")
count = 0
for line in fh:
    if count > 200:
        print ('Retrieved 200 locations, restart to retrieve more')
        break
    address = line.strip()
    print('')
    cur.execute("SELECT geodata FROM Universitylocations WHERE address= ?",(bytes[address]))
    print("Resolving", data)
    url = fh + urllib.urlencode({"sensor":"false", "address": address})
    print("Retrieving", url)
    uh = urllib.urlopen(url, context=scontext)
    data = uh.read()
    print('Retrieved',len(data),'characters',data[:20].replace('\n',''))
    count = count + 1
    try:
        js = json.loads(str(data))
    except:
        continue

    if 'status' not in js or (js['status'] != 'OK' and js['status'] != 'ZERO_RESULTS') :
        print('==== Failed to Retrieve ====')
        print (data)
        continue

    cur.execute('''INSERT INTO Universitylocations (address, geodata) VALUES (?, ?)''', (bytes[address],bytes[data]))
    conn.commit()
    if count % 10 == 0 :
        print('Pausing for a bit...')
        time.sleep(5)
print("Run process.py to read the data on a database")

Can anyone help? I've been having this issue for a while.

2
  • 2
    Include the error message in your post. This will help diagnose the issue. Commented Apr 7, 2021 at 0:48
  • accept an answer if it helped you. Commented Apr 7, 2021 at 1:09

1 Answer 1

2

This line is culprit:

cur.execute("SELECT geodata FROM Universitylocations WHERE address= ?",(bytes[address]))

change bytes[address] with (address,). Means:

cur.execute('''SELECT geodata FROM Universitylocations WHERE address= ?''',(address,))

Check what your data type is the database.

Sign up to request clarification or add additional context in comments.

3 Comments

It still doesn't work. It changes the error message into "string argument without an encoding"
No, I meant this cur.execute('''SELECT geodata FROM Universitylocations WHERE address= ?''',(address,)). Please refresh and see the answer
Please select the answer if it helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.