Skip to content

Instantly share code, notes, and snippets.

@dogmgeen
Created December 6, 2013 17:59
Show Gist options
  • Save dogmgeen/7829320 to your computer and use it in GitHub Desktop.
Save dogmgeen/7829320 to your computer and use it in GitHub Desktop.
import sys
import os
import csv
import collections
keys = [
"Timestamp",
"State",
"Company Name",
"Company Website",
"Company Type",
"Out of business?",
"Bar Name",
"Bar URL or Photo",
"Company Location",
"Currently owned?",
"Comments",
]
states = """Alabama
Alaska
American Samoa
Arizona
Arkansas
California
Colorado
Connecticut
Delaware
District of Columbia
Florida
Georgia
Guam
Hawaii
Idaho
Illinois
Indiana
Iowa
Kansas
Kentucky
Louisiana
Maine
Maryland
Massachusetts
Michigan
Minnesota
Mississippi
Missouri
Montana
Nebraska
Nevada
New Hampshire
New Jersey
New Mexico
New York
North Carolina
North Dakota
Northern Marianas Islands
Ohio
Oklahoma
Oregon
Pennsylvania
Puerto Rico
Rhode Island
South Carolina
South Dakota
Tennessee
Texas
Utah
Vermont
Virginia
Virgin Islands
Washington
West Virginia
Wisconsin
Wyoming""".split("\n")
def to_markdown_row(row):
# |State|Company Name|Company Type|Bar|Address|Currently owned?|Out of Business?|Comments|
def get_company_name(row):
return (
row["Company Name"] if not row["Company Website"]
else "[{0}]({1})".format(
row["Company Name"],
row["Company Website"]
)
)
def get_bar_details(row):
return (
row["Bar Name"] if not row["Bar URL or Photo"]
else "[{0}]({1})".format(
row["Bar Name"],
row["Bar URL or Photo"]
)
)
def is_bar_owned(row):
return "OWNED" if row["Currently owned?"] else ""
def is_out_of_business(row):
return "Defunct" if row["Out of business?"] != "" else ""
return "|{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7}|".format(
row["State"],
get_company_name(row),
row["Company Type"],
get_bar_details(row),
row["Company Location"],
is_bar_owned(row),
is_out_of_business(row),
row["Comments"],
)
if __name__ == "__main__":
if 2 > len(sys.argv):
print("Usage:\n"
" $ python3 {0} /path/to/csv".format(sys.argv[0])
)
exit(1)
results = collections.OrderedDict()
states.sort()
for s in states:
results.setdefault(s, [])
with open(sys.argv[1]) as csvfile:
company_entries = csv.DictReader(csvfile, fieldnames=keys)
next(company_entries) # skip the header
for c in company_entries:
results[c["State"]].append(to_markdown_row(c))
for state in results:
if len(results[state]) == 0:
print("|**{0}**||||||||".format(state))
else:
for r in results[state]:
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment