0

I am new to stackoverflow and python so please forgive me if this question lacks detail

I want to import data from a csv file to a table in Postgres. However, I'm not sure how to check for null values. I would like to import certain values as ints (these values for some reason have a .0 trailing them ie 10.0 when I want 10) . Some of the cells for on_hand and on_order are null though - how would I go about checking to see if these cells are null and if they are null, to simply do a row[2].strip and row[3].strip?

for row in reader:
    arg = {
        'name': row[0].strip(),
        'description': row[1].strip(),
        'on_hand': int(float(row[2].strip())),
        'on_order': int(float(row[3].strip()))
    }
    cur.execute(
        """INSERT INTO 
        "Inventory"("Name","Description","On hand","On order")
         select %(name)s, 
            %(description)s, 
            %(on_hand)s,
            %(on_order)s,
        WHERE NOT EXISTS (
                SELECT * FROM "Inventory" WHERE "Name"=%(name)s
            );""", arg)

thanks!

1
  • Were you able to solve this? Don't forget to accept an answer if your query was resolved. Commented Aug 13, 2014 at 17:24

2 Answers 2

0

How about this - basically convert to float only if row[2] actually has some value, else just return an empty string "" (You may return None as well)/

for row in reader:
    arg = {
        'name': row[0].strip(),
        'description': row[1].strip(),
        'on_hand': int(float(row[2].strip())) if row[2].strip() else "",
        'on_order': int(float(row[3].strip())) if row[3].strip() else ""
    }
5
  • why are you using int(float)? Commented Aug 1, 2014 at 20:46
  • @PadraicCunningham umm sorry, blind copy paste, corrected now. Commented Aug 1, 2014 at 20:46
  • You had me scratching my head for a minute ;) Commented Aug 1, 2014 at 20:47
  • @PadraicCunningham I was wrong earlier, check this Commented Aug 1, 2014 at 20:57
  • Ah ok, I just realized they were stored as floats Commented Aug 1, 2014 at 22:22
0

I would just try to cast and in case of a ValueError exception assign None to to the variable.

for row in reader:

    try:
        on_hand = int(float(row[2]))
    except ValueError:
        on_hand = None

    try:
        on_order = int(float(row[3]))
    except ValueError:
        on_order = None

    arg = {
        'name': row[0].strip(),
        'description': row[1].strip(),
        'on_hand': on_hand,
        'on_order': on_order
    }
4
  • You will need to move on_order also within the try block. Commented Aug 1, 2014 at 20:51
  • @mu無 Sure. Just an example. Commented Aug 1, 2014 at 20:52
  • And you are also blindly copy pasting int(float(variable)), which I too had earlier done in my answer :P Commented Aug 1, 2014 at 20:52
  • @mu無 That is necessary in case of a "doted" number like 10.0 Commented Aug 1, 2014 at 20:54

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.