Showing posts with label Study. Show all posts
Showing posts with label Study. Show all posts

Saturday, May 17, 2014

Visualize This. Ch. 2 - Switch Between Data Formats

Here are more fixed codes for Chapter 2. I use Python 3.4.0 with BeautifulSoup 4.3.2.

1. CSV to XML
import csv
reader = csv.reader(open('wunder-data.txt', 'r'), delimiter = ',')

f = open('wunder-data.xml','w')
f.write("\n")

for row in reader:
 f.write("\t\n")
 f.write("\t\t" + row[0] + "\n")
 f.write("\t\t" + row[1] + "\n")
 f.write("\t\n")
 
f.write("")

f.close()

Wednesday, May 14, 2014

Karatsuba Algorithm

This is a C code implementing Karatsuba Algorithm for integer multiplications using recursive function calls that I programmed while taking "Algorithms: Design and Analysis, Part 1" courses from free Stanford online course provided at Coursera. It's been a while since I used C, but this is a good start to stir up my old memories and get into the world of Algorithms.

Sunday, May 11, 2014

Visualize This. Ch. 2 - Data Scraping

Since the book is written in 2011, parts of Python codes in the book are no longer working in my environment (Python 3.4.0 with BeautifulSoup 4.3.2). I had to go line by line and do a log of googling to figure out what has been changed and what to do for those changes. This is for my future reference, and hopefully others can find this helpful when they study with this book.
import urllib2 urllib.request
from BeautifulSoup bs4 import BeautifulSoup
print "Getting data for" + timestamp ("Getting data for " + timestamp)
page = urllib2.urlopen(url) urllib.request.urlopen(url)
dayTemp = soup.findAll(attrs={"class":"nobr wx-value"})[5 1].span.string
It works like a charm!