0

My stock programs input is as follow

'Sqin.txt' data read in and is a cvs file

AAC,D,20111207,9.83,9.83,9.83,9.83,100
AACC,D,20111207,3.46,3.47,3.4,3.4,13400
AACOW,D,20111207,0.3,0.3,0.3,0.3,500
AAME,D,20111207,1.99,1.99,1.95,1.99,8600
AAON,D,20111207,21.62,21.9,21.32,21.49,93200
AAPL,D,20111207,389.93,390.94,386.76,389.09,10892800
AATI,D,20111207,5.75,5.75,5.73,5.75,797900

The output is

 dat1[]
['AAC', ['9.83', '9.83', '9.83', '9.83', '100'], ['9.83', '9.83', '9.83', '9.83', '100']]

dat1[0] is the stock symbol 'ACC' used for lookup and data updates Dat1[1....?] Is the EOD (end of day) data At the close of stock markets the EOD data will be inserted at dat1.insert (1,M) each update cycle . Guys you can code this out in probably one line. Mine so far is over 30 lines, so seeing my code isn't relevant. Above is an example of some simple input and the desired output.

If you decide to take on some real world programing please keep it verbose. Declare your variables, then populate it, and finally use them ex.

M = []
M = q [0][3:]  ## had to do it this way because 'ACC' made the variable M [] begin as a string (inmutable).  So I could not add M to the data.-dat1[]- because -dat1[]- also became a string (inmutable strings how stupid). Had to force 'ACC' to be a list so I can create a list of lists -dat1-

Dat1.insert(1.M)  ## -M- is used to add another list to the master.dat record

Maybe it would be OK to be some what pythonic and a little less verbose.

Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
tsquare21
  • 53
  • 4
  • Is this homework? Is it a Python course, and if so, does your professor really think you can declare variables in python? – Wooble Jan 28 '12 at 22:59
  • Tried m = q[0] ## that's 'ACC" m turned into a string as I said "INMUTABLE" so if I start out with a string how do I add more data to my database when m initiates it as a string. So adding dat1.insert(1,m) will throw an exception with INMUTABLES. If this was about school my question would be something as List2[1,1,1,1,1,1,1], List2[2,2,2,2,2} with an output of List {1.1.2.1.1.1.2.2.1.1.1.1.2.2.2.2} or some "REAL" world application that most in here find relevant. I have only found one other example, out of many, that you might say is a real world problem. – tsquare21 Jan 29 '12 at 00:52

1 Answers1

0

You should use a dictionary with the names as keys:

import csv
import collections

filename = 'csv.txt'

with open(filename) as file_:
    reader = csv.reader(file_)
    data = collections.defaultdict(list)
    for line in reader:
        # line[1] contains "D" and line[2] is the date
        key, value = line[0], line[3:]
        data[key].append(value)

To add data you do data[name].insert(0, new_data). Where name could be AAC and value is a list of the data. This places the new data at the beginning of the list like you said in your post.

I would recommend append instead of insert, it is faster. If you really want the data added to the begin of the list use collections.deque instead of list.

Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
  • Thanks rob Ideally would like to us a header for database with [name.stk1,stk2,stk3........stk?] And this would be used as the dictionary keys. Database keys not important for searching file or updating. What is necessary is that the program can access each days entries. The entries will be used in various formulas for processing. All processing is sequentially through the list until EOF is reached. What is important is that stk1[3] > stk5[3] or some formula then, write (reportx) . – tsquare21 Jan 29 '12 at 00:51
  • My code does exactly what your question requested. If you want to search for date I would have a dict of dicts where the first key is the name and the second key is the date. But you should really be more clear, right now it's just some rambling about supposed "real world" programming. – Rob Wouters Jan 29 '12 at 13:14