1

I have a postgres table with below structure

CREATE TABLE edmonton.permit
                            (permit_date text,
                             permit_number text,
                             year numeric,
                             month_number integer,
                             report_permit_date text,
                             job_category text,
                             address text,
                             legal_description text,
                             neighbourhood text,
                             neighbourhood_number text,
                             job_description text,
                             building_type text,
                             work_type text,
                             floor_area numeric,
                             construction_value integer,
                             zoning text,
                             units_added numeric,
                             latitude double precision,
                             longitude double precision,
                             location text,
                             count integer

I am trying to read a csv file from a relative path and import data into postgres table like below

                with open(file) as f:
                reader = csv.reader(f, delimiter=',')
                next(reader, None)
                for row in reader:
                    cur.execute("INSERT INTO edmonton.{}(permit_date, permit_number, year, month_number, report_permit_date, job_category, address, legal_description, neighbourhood, neighbourhood_number, job_description, building_type, work_type, floor_area, construction_value, zoning, units_added, latitude, longitude, location, count) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".format(permit),row)
                    con.commit()

I am getting the below error

Invalid literals for numeric datatype.

What place holders should be used for numeric,integer,double precision using python3.4?

1
  • The placeholders are %s for everything with Psycopg. You need to show the csv. Commented Mar 7, 2017 at 14:46

1 Answer 1

1

This might help to narrow it down. Will attempt to format each field and give error on the one causing an issue.

with open(file) as f:
reader = csv.reader(f, delimiter=',')
next(reader, None)
for row in reader:
    for item in row:
        print cursor.mogrify("%s", (item,))
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.