CSV data into postgreSQL using Python

Jake Wagner

I am trying to import CSV data into postgreSQL using Python. I tried to search on Stack Overflow for this issue but couldn't find anything fruition for my situation. I have empty columns in my CSV file, when I run the code it throws out an error for the blank column for which there's no information. I want to be able to tell Python to ignore the blank column and continue to the next column. Not all of the columns have data in them so I want your kind assistance to implement a solution which I can have edited in the script for the columns which have no data in them. I am new to Programming so please pardon my stupidity.

import psycopg2
import csv

csv_data = csv.reader(file('SampleData1.csv'))

database = psycopg2.connect (database = "**", user="**", password="***", host="**", port="**")

cursor = database.cursor()
delete = """Drop table if exists "Real".SampleDataOne"""
print (delete)

mydata = cursor.execute(delete)

cursor.execute("""Create Table "Real".SampleDataOne
              (Username varchar(55),
              TimeStamp timestamp,
              Week date,
              Actual_Sale_Date date,
              Estimated_Closing_Date date, 
              Last_Name_First_Name varchar(55),
               Ages varchar(55)
               );""")

print "Table created successfully" 

next(csv_data)
for row in csv_data:

cursor.execute("""Insert into "Real".SampleDataOne(Username,TimeStamp,  Week,   Actual_Sale_Date, Estimated_Closing_Date, \
               Last_Name_First_Name,   Ages)"""\
               """VALUES (%s,%s,%s,%s,%s,%s,%s)""",
               row)

cursor.close()
database.commit()
database.close()

print "CSV imported"

The error is as follows: It has a point upwards (^) next to the column after the 'False'.

Drop table if exists "Real".SampleDataOne
Table created successfully

Traceback (most recent call last):
File "C:/Users/Desktop/Programming/SampleData1Python.py", line 61,   in <module>
row)
DataError: invalid input syntax for type date: ""
LINE 1: ...****@jcp.com','****@comcast.net','No','FALSE','','','')
BmoreAGG

http://initd.org/psycopg/docs/cursor.html#cursor.copy_from

f = open('SampleData1.csv')
cursor.copy_from(f, '"Real".sampledataone', sep=',', null='')

Should work provided you have simple csv data without quoting or commas in the data.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

copy data from csv to postgresql using python

From Dev

Exporting a PostgreSQL query to a csv file using Python

From Dev

Python if statement using CSV Data

From Dev

Using Python to group csv data

From Dev

Data not being saved in Postgresql using Python

From Dev

how to export data from postgresql to .csv file using jdbc?

From Dev

how to push a csv data to mongodb using python

From Dev

Importing data from CSV to MySQL using python

From Dev

Save data to csv file using Python

From Dev

Data structure for heterogeneous csv using python

From Dev

Loading CSV data to a PostgreSQL table

From Dev

CSV file data into a PostgreSQL table

From Dev

CSV file data into a PostgreSQL table

From Dev

Extract column data from a CSV file using Python

From Dev

Python-xlsxwriter only writes as a text when using csv data

From Dev

Importing financial data into Python Pandas using read_csv

From Dev

Align column names with data in a CSV file using Python

From Dev

Using Python to manipulate data from a CSV only applying it to the first result

From Dev

Extracting variable names and data from csv file using Python

From Dev

how to create 1 gb csv file with random data using python

From Dev

how to write nested json data to CSV using python

From Dev

Exporting data as CSV file from ServiceNow instance using Python

From Dev

Extract column data from a CSV file using Python

From Dev

How to check if an data already present in a CSV file using Python

From Dev

Using Python to manipulate data from a CSV only applying it to the first result

From Dev

Extracting variable names and data from csv file using Python

From Dev

How to Alter returned data format called from csv using python

From Dev

Split CSV file using Python shows not all data in Excel

From Dev

Use dictwriter and write unstructured data into csv file using python

Related Related

  1. 1

    copy data from csv to postgresql using python

  2. 2

    Exporting a PostgreSQL query to a csv file using Python

  3. 3

    Python if statement using CSV Data

  4. 4

    Using Python to group csv data

  5. 5

    Data not being saved in Postgresql using Python

  6. 6

    how to export data from postgresql to .csv file using jdbc?

  7. 7

    how to push a csv data to mongodb using python

  8. 8

    Importing data from CSV to MySQL using python

  9. 9

    Save data to csv file using Python

  10. 10

    Data structure for heterogeneous csv using python

  11. 11

    Loading CSV data to a PostgreSQL table

  12. 12

    CSV file data into a PostgreSQL table

  13. 13

    CSV file data into a PostgreSQL table

  14. 14

    Extract column data from a CSV file using Python

  15. 15

    Python-xlsxwriter only writes as a text when using csv data

  16. 16

    Importing financial data into Python Pandas using read_csv

  17. 17

    Align column names with data in a CSV file using Python

  18. 18

    Using Python to manipulate data from a CSV only applying it to the first result

  19. 19

    Extracting variable names and data from csv file using Python

  20. 20

    how to create 1 gb csv file with random data using python

  21. 21

    how to write nested json data to CSV using python

  22. 22

    Exporting data as CSV file from ServiceNow instance using Python

  23. 23

    Extract column data from a CSV file using Python

  24. 24

    How to check if an data already present in a CSV file using Python

  25. 25

    Using Python to manipulate data from a CSV only applying it to the first result

  26. 26

    Extracting variable names and data from csv file using Python

  27. 27

    How to Alter returned data format called from csv using python

  28. 28

    Split CSV file using Python shows not all data in Excel

  29. 29

    Use dictwriter and write unstructured data into csv file using python

HotTag

Archive