PythonLearner PythonLearner - 1 month ago 17
Python Question

Skip the first line in Python

I am importing CSV data into postgreSQL using Python. I need to tell Python to skip the first line of the CSV. I looked into posts on the Stack Overflow and tried the codes and snippets suggested but no fruition. Can someone please provide assistance.

import psycopg2
import csv

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

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

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

mydata = cursor.execute(delete)

cursor.execute("""Create Table Real.SampleDataTwo
(Username varchar(55),
LastUpdate timestamp,
Week date,
Builder varchar(55),
Traffic integer
);""")

print "Table created successfully"

for row in csv_data:

cursor.execute("INSERT INTO Real.SampleDataTwo (Username, LastUpdate, Week, Builder, Traffic)"\
"VALUES (%s,%s,%s,%s,%s)",
row)


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

print "CSV data imported"

Answer

Call next on csv_data before iterating through it:

next(csv_data)
for row in csv_data:
    ...
Comments