[Chicago] Using split to arrays

Dan Krol orblivion at gmail.com
Mon Mar 12 17:43:27 CET 2012


Couple things to help clarify:

1: You're importing the csv library, but you're not using it. If you use
it, it'll take care of all the comma parsing for you, probably better than
you or I could think to do it. http://docs.python.org/library/csv.html

2: I'm not sure what exactly you're asking for. Execution is a list of
lists. You want just one list of data items? Do you want to append the
lists in execution into one list or something? Maybe give an example of the
sort of output you're looking for?

On Mon, Mar 12, 2012 at 11:37 AM, Freddy Martinez <freddymartinez9 at gmail.com
> wrote:

> Greetings ChiPy,
>
> I'm having an issue reading a file into two arrays (or one
> multi-dimensional array):
>
> The file is formatted like this (a simple comma delimited text file).
>
> 09:30:17,100,120.42,,
> 09:30:31,600,120.31,100,
> 09:30:37,600,120.31,,
> 09:30:47,500,120.3,,
> 09:30:50,100,120.3,,
> 09:30:50,100,120.2985,,
>
> I need each data field to be its own array element. (which is where
> I'm having a problem).  I can read each in line and create a list
> array, but I can't use the split function on a list array.
>
> The code is as follows:
>
> import csv
> import collections
> d = collections.defaultdict(int)
> execution=[]
> details=[]
> data=[]
>
> marguse=0
> avlmargin=0
>
> execution=open('/home/freddy/Margin2.txt'.strip(),'r')
>
> for line in execution:
>    linedat=line.strip().split(",")
>    details.append(linedat)
>
> print details[0]
>
> The code output is as such: ['09:30:17', '100', '120.42', '', ''] but
> it is a an array of lists. I'm trying to figure out the best way to
> iterate through the execution array and create another array. I'm
> fairly sure its the way I am opening and reading the file but I'm not
> sure.
>
> Ideas?
>
> Thanks in advance.
>
> Freddy
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20120312/b4ce796b/attachment.html>


More information about the Chicago mailing list