6

I have a program when it currently reads from a database, which can be found here. I have users choose a specific record they want to display so the SQL command will execute that record. Now I have a table that currently displays some records that do not include any NULL or empty strings. If it does have NULL or empty strings, it gives me an error and the program does not display the records. I figured that is where the NoneType error is mostly coming from. I'm not sure how to fix that. How can I make sure it also counts the Null or empty strings? Hopefully, that will fix the error.

If you were to try and test the DB, tables like Customers don't display because it has null values.

Here is the Traceback error:

line '..', in read_display(record)
    line = format_.format(*rec)
TypeError: unsupported format string passed to NoneType.__format__

This is what my code looks like:

import sqlite3

def read_display(record):
  database = 'data.db'
  connection = sqlite3.connect(database)
  c = connection.cursor()
  sql = "SELECT * FROM {0}".format(record)
  cursor.execute(sql)
  conn.commit()
  results = cursor.fetchall()

  header = tuple(i[0] for i in c.description)
  width = max((len(str(x)) for d in data for x in d))

  data = [header] + results

  config = [{'width': 0} for _ in range(len(data[0]))]

  for rec in data:
    for c, value in enumerate(rec):
        config[c]['width'] = max(config[c]['width'], len(str(value)))


  format_ = []
  for f in config:
    format_.append('{:<' + str(f['width']) + '}')

  format_ = ' | '.join(format_)
  for rec in data:
    line = format_.format(*rec)
    print(line)
6
  • check print(format_, rec) before line = format_.format(*rec) - it seams format_ or rec is None. You can get the same error with '{:s}'.format(None) Commented May 3, 2019 at 5:11
  • @furas when I used my debugger, it seemd like format_ does not read the None because I don't think it recognizes None as a string. It skips the other characters and goes to the next string like 'SupplierID' and counts it as "{:<10}" or what not. Commented May 3, 2019 at 5:30
  • 4
    None is not a string so it can't recognize it as a string. You would have to convert it to string str(None). You can use str() for all elements to make sure: rec = [str(x) for x in rec] Commented May 3, 2019 at 5:34
  • should I put the rec = [str(x) for x in rec] before the line = format_.format(*rec) to make None a string? Commented May 3, 2019 at 5:37
  • 2
    yes. You could try it before you ask. This way you can also learn something. And you don't have to wait for my answer. Commented May 3, 2019 at 5:39

1 Answer 1

13

You have error in

line = format_.format(*rec)

and you can get the same error with

'{:s}'.format(None)

so it seems rec has None on the list.

You would have to convert it to string with str(None)

You can use str() with all elements in rec to make sure:

 rec = [str(x) for x in rec]

So code should be

for rec in data:
    rec = [str(x) for x in rec]
    line = format_.format(*rec)
Sign up to request clarification or add additional context in comments.

Comments

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.