-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnew books list_csv.py
More file actions
33 lines (24 loc) · 898 Bytes
/
new books list_csv.py
File metadata and controls
33 lines (24 loc) · 898 Bytes
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
#!/usr/bin/env python3
"""Create and email a list of new items
Jeremy Goldstein
Minuteman Library Network
Example for Automating booklist curation with SQL presentation given at IUG2019
"""
import psycopg2
import csv
from datetime import date
#Name of Excel File
csvFile = 'NewBookList.csv'.format(date.today())
#Connecting to Sierra PostgreSQL database
#Add in the credentials for your database
conn = psycopg2.connect("dbname='' user='' host='' port='1032' password='' sslmode='require'")
#Opening a session and querying the database for weekly new items
cursor = conn.cursor()
cursor.execute(open("new books list.sql","r").read())
#For now, just storing the data in a variable. We'll use it later.
rows = cursor.fetchall()
conn.close()
with open(csvFile,'w', encoding='utf-8') as tempFile:
myFile = csv.writer(tempFile, delimiter='|')
myFile.writerows(rows)
tempFile.close()