-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsa.py
78 lines (49 loc) · 1.85 KB
/
sa.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
"""Move data to a database with SQLAlchemy
Script demonstrating how to move data from a CSV file to a database
using SQLAlchemy.
"""
import csv
from dateutil.parser import parse
from sqlalchemy import Column, Date, Float, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine("sqlite:///ab_nyc.sqlite3", echo=True)
Base = declarative_base()
class Listing(Base):
"""SQLAlchemy mapped class. Maps a Listing object to the corresponding
database table."""
__tablename__ = "listings_sqlalchemy"
id = Column(Integer, primary_key=True)
name = Column(String(200))
host_id = Column(Integer)
host_name = Column(String(50))
neighbourhood_group = Column(String(20))
neighbourhood = Column(String(20))
latitude = Column(Float)
longitude = Column(Float)
room_type = Column(String(20))
price = Column(Integer)
minimum_nights = Column(Integer)
number_of_reviews = Column(Integer)
last_review = Column(Date, nullable=True)
reviews_per_month = Column(Integer)
calculated_host_listings_count = Column(Integer)
availability_365 = Column(Integer)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
def parse_none(dt):
"""Trys to parse a string date and returns None if unable to."""
try:
return parse(dt)
except:
return None
def prepare_listing(row):
"""Takes a row from CSV file and returns a Listing object from it."""
row["last_review"] = parse_none(row["last_review"])
return Listing(**row)
with open("AB_NYC_2019.csv", encoding="utf-8", newline="") as csv_file:
csvreader = csv.DictReader(csv_file, quotechar='"')
listings = [prepare_listing(row) for row in csvreader]
session = Session()
session.add_all(listings)
session.commit()