Python help

I want to learn a little programming and have decided to start with Python.

As a first step I would like read a csv file containing date/open/high/low/close into an array or list. I've searched the python docs and google and can't find any code examples i can get started with.

Any tips to point me in the right direction appreciated.
 
zentrader,

I'm no python expert, but I think something like the following will get you going. Please note, I put periods in instead of spaces for the tabs since the forum would strip the leading spaces. Also, I attached the source file to the message.
----

file = open('c:\somefile.csv', 'r')

lineStr = file.readline()

while lineStr:
........# pieces will be an array with your individual components in it
........pieces = lineStr.split(',')
........lineStr = file.readline()
 

Attachments

Here's some code I had lying around which parses Yahoo historical data in csv format. It does some rudimentary error checking.

I used < pre > tag to get a fixed width font but it seems to double space the lines. Anyone know a better way?

Martin
<pre>
def ReadCsv(csvFileName):
# read in data file
try:
csvFile = open(csvFileName)
csvData = [line.strip().split(",") for line in csvFile.readlines()]
csvFile.close()
except IOError:
return []

# check for HTML file (bad data)
if len(csvData) >= 2 and '404 Not Found' in csvData[1][0]:
return []

# extract lines with dates only, and get rid of adjusted close
return [line[0:6] for line in csvData if len(line[0].split("-"))==3]
</pre>
 
Python resources:

Mark Pilgrim's "Dive into Python" is a good tutorial which is available online here:

http://diveintopython.org/toc/index.html

"Python in a Nutshell" by Alex Martelli isn't available online, but it covers all the bases. Well worth owning. I have a few Python books but this is the only one I use regularly.

Martin
 
Quote from Sparohok:


I used < pre > tag to get a fixed width font but it seems to double space the lines. Anyone know a better way?

Use the
Code:
 tag
 
Thanks for the help guys. I will play around with the code and go through that help doc and I should be able to work it out.
 
I want to end up with 5 arrays titled Date/Open/High/Low/Close. I can't work out how to achieve this using readline or the CSV module. I am sure there must be some simple code for this?
 
Code:
dateList = openList = highList = lowList = closeList = []
for (date,open,high,low,close,vol) in ReadCsv(csvFileName):
    dateList.append(date)
    openList.append(open)
    highList.append(high)
    lowList.append(low)
    closeList.append(close)

Haven't tested it but it should work.

Another way using list comprehensions:

Code:
results = ReadCsv(csvFileName)
dateList = [d for (d,o,h,l,c,v) in results]
openList = [o for (d,o,h,l,c,v) in results]
# and so forth

I would encourage you to spend some time learning the fundamental Python language constructs. Learning by doing is great, but starting with a good tutorial is probably a good idea.

Martin

p.s. Thanks dcraig!
 
Back
Top