Skip to content

Instantly share code, notes, and snippets.

import json
import sqlite3
conn = sqlite3.connect('rosterdb.sqlite')
cur = conn.cursor()
# Do some setup
cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Member;
import xml.etree.ElementTree as ET
import sqlite3
conn = sqlite3.connect('trackdb.sqlite')
cur = conn.cursor()
# Make some fresh tables using executescript()
cur.executescript('''
DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Genre;
@manichabba
manichabba / Sqlite.py
Created August 23, 2016 20:45
Counting Organizations This application will read the mailbox data (mbox.txt) count up the number email messages per organization (i.e. domain name of the email address) using a database with the following schema to maintain the counts.
import re
import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('''
DROP TABLE IF EXISTS Counts''')
@manichabba
manichabba / geoJSON.py
Created August 20, 2016 03:21
=====Calling a JSON API=====The program will prompt for a location, contact a web service and retrieve JSON for the web service and parse that data, and retrieve the first place_id from the JSON. A place ID is a textual identifier that uniquely identifies a place as within Google Maps.
import urllib
import json
serviceurl = 'http://python-data.dr-chuck.net/geojson?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
@manichabba
manichabba / jsonscraping.py
Created August 20, 2016 01:17
====Extracting Data from JSON: ==== The program will prompt for a URL, read the JSON data from that URL using urllib and then parse and extract the comment counts from the JSON data, compute the sum of the numbers in the file and provide the sum.
import urllib #importing urllib
import json #importing json
#requesting a json file url
url = raw_input("Enter the URL:")
#load json file as list -info
info = json.loads(urllib.urlopen(url).read())
x = 0
#loop through each item in list comments
@manichabba
manichabba / xmlscraping.py
Created August 19, 2016 14:39
Extracting Data from XML: The program will prompt for a URL, read the XML data from that URL using urllib and then parse and extract the comment counts from the XML data, compute the sum of the numbers in the file.
import urllib #importing urllib
import xml.etree.ElementTree as ET #importing xml library
url = raw_input("Enter the URL:") #requesting a xml file
#read the file and get the comment tag
data = urllib.urlopen(url).read()
tree = ET.fromstring(data)
lst = tree.findall('.//comment')
@manichabba
manichabba / NumScrap.py
Created August 15, 2016 17:34
Number scraping using beatiful soup from content of span tag
import urllib
from BeautifulSoup import *
url = raw_input("Enter the URL:")
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags=soup('span')
x = list()
@manichabba
manichabba / WritingWeb.py
Created August 15, 2016 14:42
Using sockets to read a URL and get information
import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.pythonlearn.com', 80))
mysock.send('GET http://www.pythonlearn.com/code/intro-short.txt HTTP/1.0\n\n')
while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
@manichabba
manichabba / FindNumber.py
Created August 15, 2016 14:10
Finding Numbers in a Haystack In this assignment you will read through and parse a file with text and numbers. You will extract all the numbers in the file and compute the sum of the numbers.
import re #using regular expressions
print sum(map(int, re.findall('[0-9]+',open('RegexSum289081.txt').read())))
#Outline-read the file, look for integers using the re.findall(),converting the extracted strings to integers and summing up the integers.