[Chicago] Using split to arrays

Kody Crouch kccrouch at uchicago.edu
Mon Mar 12 17:55:09 CET 2012


Not completely understanding the question, but maybe you want to use + to
merge the lists instead of append to append a list

for line in execution:
   linedat=line.strip().split(",")
   details += linedat





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/ac2ff4c2/attachment.html>


More information about the Chicago mailing list