-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_read_data_from_csv.py
142 lines (108 loc) · 3.51 KB
/
10_read_data_from_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
import psycopg2
from config import config
import csv
"""
Not working so far
"""
def read_from_csv(path_to_file):
""" insert a csv into a table """
conn = None
try:
# read data from a picture
part_list = open(path_to_file, 'r') #.read()
# read database configuration
params = config()
# connect to the PostgresQL database
conn = psycopg2.connect(**params)
# create a new cursor object
cur = conn.cursor()
#with open(path_to_file, 'r') as f:
# next (f) #skip headre
# cur.copy_from(f, 'parts', sep=',')
cur.copy_from(part_list, 'parts', sep=',')
# commit the changes to the database
conn.commit()
# close the communication with the PostgresQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def read_from_csv_vendors(path_to_file):
""" insert a csv into a table """
conn = None
try:
# read database configuration
params = config()
# connect to the PostgresQL database
conn = psycopg2.connect(**params)
# create a new cursor object
cur = conn.cursor()
with open(path_to_file, 'r') as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute(
"INSERT INTO vendors VALUES (%s, %s)",
row
)
conn.commit()
# close the communication with the PostgresQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def read_from_csv_users(path_to_file):
""" insert a csv into a table """
conn = None
try:
# read database configuration
params = config()
# connect to the PostgresQL database
conn = psycopg2.connect(**params)
# create a new cursor object
cur = conn.cursor()
#f = open(path_to_file, 'r')
#cur.copy_from(f, 'users', sep=',')
with open(path_to_file, 'r') as f:
reader = csv.reader(f)
next(reader) # Skip the header row.
for row in reader:
cur.execute(
"INSERT INTO users VALUES (%s, %s, %s, %s)",
row
)
f.close()
conn.commit()
# close the communication with the PostgresQL database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
if __name__ == '__main__':
# read database configuration
params = config()
# connect to the PostgresQL database
conn = psycopg2.connect(**params)
# create a new cursor object
cur = conn.cursor()
cur.execute("""
CREATE TABLE users(
id integer PRIMARY KEY,
email text,
name text,
address text
)
""")
conn.commit()
# close the communication with the PostgresQL database
cur.close()
#read_from_csv (path_to_file='parts.csv')
#read_from_csv_users (path_to_file='vendors.csv')
read_from_csv_users (path_to_file='users.csv')