Category Archives: Python

Python to download all tweets from Twitter

import urllib2 username = ” password = ” format = ‘json’ # json or xml filename = ‘archive.json’ # filename of the archive tweets = 164 # number of tweets pages = (int(float(tweets)/float(80)))+1 auth = urllib2.HTTPPasswordMgrWithDefaultRealm() auth.add_password(None, ‘http://twitter.com/account/’, username, password) … Continue reading

Posted in Python | Comments Off

Python Stock Program – Another One

There is a file called “portfolio.dat”, containing the stock symbol, the number of shares, and the cost basis purchase price, all separated by “|” (but you can change it to commas). This program requires yahoo quote. The source is : … Continue reading

Posted in Finance, Python | Comments Off

Python Stock Program

Python Historical Stock Program This is a program downloaded from http://www.hotscripts.com/Python/Scripts_and_Programs/Financial_Tools/index.html The program is installed whereever you want. The following are instructions: 1. Create a folder called ‘rawdata’. This is where the downloaded csv results files will go 2. Change … Continue reading

Posted in Finance, Python | Comments Off

Python Google Stock Quotes

import urllib import re def get_quote(symbol): base_url = ‘http://finance.google.com/finance?q=’ content = urllib.urlopen(base_url + symbol).read() m = re.search(‘class=”pr”.*?>(.*?)

Posted in Computer, Python | Comments Off

Python: Processing Lines in a File

A Python program that searches for text in a file must loop through the file contents on a line by line basis: file = open(‘/tmp/filename’, ‘r’) while True: line = file.readline() if line == “”: break # Check for end-of-file … Continue reading

Posted in Python | Comments Off

Python File Renamer

import os illegal = list(”’!”#$%&\’()*+,/:;?@[\\]^_`{|}~”’) repl = ” ” for root,dir,files in os.walk(os.getcwd()): for fi in files: for ill in illegal: if ill in fi: print “found “, os.path.join(root,fi) newname = fi.replace(ill,repl) os.rename( os.path.join(root,fi) , os.path.join(root,newname) )

Posted in Python | Comments Off