Skip to content

Instantly share code, notes, and snippets.

@rcackerman
Created July 24, 2012 18:53
Show Gist options
  • Select an option

  • Save rcackerman/3171831 to your computer and use it in GitHub Desktop.

Select an option

Save rcackerman/3171831 to your computer and use it in GitHub Desktop.
import csv, ast, re
with open("/Users/rackerman/Projects/provider directory scrapers/d2h_listings.txt") as infile:
listings = ast.literal_eval(infile.read())
with open("/Users/rackerman/Projects/provider directory scrapers/d2h.csv", "w") as outfile:
fieldnames = ["Program Contact", "Program/Facility Location", "Total Number of Units",
"Housing Type", "Services", "Location(s)", "Sobriety Requirement", "Populations served",
"Eligibility requirements", "How to apply", "Required documents", "Additional information"]
outfile.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)
w = csv.DictWriter(outfile,fieldnames=fieldnames)
w.writeheader()
for lst in listings:
for k,v in lst.items():
# Getting everything pretty for making a csv
if v is None:
v = u"None"
elif isinstance(v, list):
v = " ".join(v)
else:
pass
k = re.sub(":", "", k)
# w.writerow({k:v.encode('utf8') for k,v in lst.items()})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment