table from csv file
I am trying to create a table in python from a csv file where I input
which columns I would like to see, and the table only shows those
columns. I have attached an example of the csv file i am using, and
some of the code I have written. I am having trouble converting
variables between lists, dictionaries and tuples. Is there a name for
what I am attempting to do? any help to get me on the right track with
this is appreciated.
test.csv
DateOpen HighLowClose Volume Adj Close
12/14/09110.01 110.7 109.5 110.24 16316000110.24
12/11/09110.6 110.82 108.72 109.32 28983100109.32
12/10/09110.66 111.03 110.04 110.82 20491700110.82
12/9/09 111.6 112.48 109.38 110.84 37104900110.84
12/8/09 112.76 113.06 110.21 110.95 37630800110.95
12/7/09 111.51 114.22 111.44 113.11 42480500113.11
12/4/09 117.15 117.26 112.41 113.75 79182600113.75
12/3/09 118.57 119.54 118.03 118.7 28802100118.7
12/2/09 118.8 119.27 118.3 119.18 30994600119.18
12/1/09 117.3 117.93 116.78 117.38 27641000117.38
11/30/09114.48 115.89 114.27 115.64 16402300115.64
11/27/09113.08 115.81 113.02 115.06 21234400115.06
11/25/09115.69 116.88 115.53 116.62 24553300116.62
11/24/09114.73 114.81 113.97 114.73 22599700114.73
11/23/09114.67 115.12 113.99 114.29 24422700114.29
11/20/09111.74 112.94 111.54 112.94 17302500112.94
11/19/09111.85 112.4 110.76 112.3 21239800112.3
11/18/09112.69 113.09 111.8 112.25 22320600112.25
11/17/09111.09 111.99 110.9 111.97 19732900111.97
11/16/09110.7 112.16 110.65 111.63 25002300111.63
11/13/09108.32 109.8 108.14 109.74 17246000109.74
11/12/09109.16 109.56 108.12 108.21 17848300108.21
11/11/09109.49 109.71 109 109.6 17654100109.6
11/10/09108.03 108.78 107.7 108.39 15973300108.39
11/9/09 108.69 108.75 107.91 108.19 18444800108.19
11/6/09 107.38 108.04 107.06 107.43 14789000107.43
11/5/09 106.81 107.2 106.6 106.98 10189000106.98
11/4/09 107.11 107.68 106.43 107.1 27125500107.1
table.py;
import csv
(D, O, H, L, C, V, A) = (11, 'open', 'high', 'low', 'close', 66, 77)
d = {'high':H, 'low':L, 'close':C, 'open':O}
spacing = '%-*s'
w=raw_input('what do you want to see? (use commas to seperate values)
')
y=w.lower()
x=y.replace(' ','')
print x
p = x.split(',')#this takes string, converts to
list
print p
num = len(p)
format = num*spacing
width = 12*num
wi = 12
secwidth = width - wi
bb = []
i=0
while i < num: #creates new list with variables
ll = d[p[i]]
bb.insert(i,ll)
i+=1
print bb
i = 0
while i < num:
bb.insert(i*2, 12) #this works on list
i+=1
print bb
i = 0
while i < num:
p.insert(i*2, 12) #this works on list
i+=1
q = tuple (p) #takes list, converts to tuple
reader = csv.reader(open('/prog/test.csv', "rb"))
rownum = 0
for row in reader:
if rownum == 0:
print '=' * width
print format % (q)
print '-' * width
print ''
else:
D, O, H, L, C, V, A = row [:7]
o = tuple (bb)
print format % (o)
rownum += 1
--
http://mail.python.org/mailman/listinfo/python-list
syntax
I wrote this program, but i have a feeling like there might be a more
practical way of writing it. Can someone give me an idea of how to
simplify this? Here is an example of the csv file i am using. This
program calculates the exponential moving average of the 20 day range.
USOtable.csv (full table found at
http://ichart.finance.yahoo.com/table.csv?s=USO)
Date,Open,High,Low,Close,Volume,Adj Close
2010-01-15,38.97,39.02,38.28,38.40,12615300,38.40
2010-01-14,39.30,39.44,38.88,39.06,8575900,39.06
2010-01-13,39.40,39.71,38.63,39.21,15502700,39.21
2010-01-12,40.07,40.36,39.53,39.63,11960100,39.63
2010-01-11,41.09,41.19,40.46,40.54,8902200,40.54
2010-01-08,40.63,41.17,40.45,40.93,9393500,40.93
2010-01-07,40.87,41.08,40.68,40.72,10012000,40.72
2010-01-06,40.32,41.19,39.89,40.97,19789800,40.97
2010-01-05,40.25,40.45,39.93,40.41,10450200,40.41
test.py
import csv
reader = open('/prog/USOtable.csv','rb')
data = [row for row in csv.reader(reader)]
Nvals=
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
H=float(data[40][2])+float(data[41][2])+float(data[42][2])+float(data
[43][2])\
+float(data[44][2])+float(data[45][2])+float(data[46][2])+float(data
[47][2])\
+float(data[48][2])+float(data[49][2])+float(data[50][2])+float(data
[51][2])\
+float(data[52][2])+float(data[53][2])+float(data[54][2])+float(data
[55][2])\
+float(data[56][2])+float(data[57][2])+float(data[58][2])+float(data
[59][2])\
+float(data[60][2])
L=float(data[40][3])+float(data[41][3])+float(data[42][3])+float(data
[43][3])\
+float(data[44][3])+float(data[45][3])+float(data[46][3])+float(data
[47][3])\
+float(data[48][3])+float(data[49][3])+float(data[50][3])+float(data
[51][3])\
+float(data[52][3])+float(data[53][3])+float(data[54][3])+float(data
[55][3])\
+float(data[56][3])+float(data[57][3])+float(data[58][3])+float(data
[59][3])\
+float(data[60][3])
Nvals[39]=(H-L)/2
i=1
while i <=38:
high=float(data[39-i][2])
low=float(data[39-i][3])
TR=high-low
Nvals[39-i]=(19*Nvals[40-i]+TR)/20
i+=1
for value in Nvals:
print value
--
http://mail.python.org/mailman/listinfo/python-list
Re: syntax
oh, and i need to make those a new column that is added to the csv file. Thanks -- http://mail.python.org/mailman/listinfo/python-list
