python-noob - which container is appropriate for later exporting into mySql + matplotlib ?

someone newsboost at gmail.com
Thu Apr 11 04:49:59 EDT 2013


On 2013-04-11 03:39, Cousin Stanley wrote:
> for row in list_tuples :
>
>      print '  ' , row.date , row.time , row.col1 , row.col3 , row.col4
>
> file_source.close()

Oh, that's great - thank you - I didn't know this named-tuple container 
before... I'm still wondering whether or not it's the optimal container 
type for me, because I just added a bit of matplotlib-code:

-----------------
#!/usr/bin/env python

import csv
from collections import namedtuple as NT

file_source = open( 'someone.csv' )
# -------------------> individual column names ---------------
nt = NT( 'csv_data' , 'date time col1 col2 col3 col4 col5 col6' )
list_tuples = [ ]
for this_row in csv.reader( file_source ) :
     # unpack the current row
     zed , one , two , tre , fur , fiv , six = this_row
     # split the date and time
     d , t = zed.split( 'T' )
     # convert individual columns in row to a named tuple
     this_tuple = nt( d ,
                      t ,
                      float( one ) ,
                      int( two ) ,
                      int( tre ) ,
                      float( fur ) ,
                      int( fiv ) ,
                      int( six ) )
     # save the current named tuple into a list
     list_tuples.append( this_tuple )
     # update_data_base( this_tuple )
     # .... or ....
     # update_data_base( choose individual columns )

file_source.close()
# individual elements of the named tuples
# can be accessed by name
#
# this might be convenient for settup up
# data for plots of diffeent columns

x=[]
y=[]
print
for row in list_tuples :
     print '  ' , row.date , row.time , row.col1 , row.col3 , row.col4
     x.append(row.col3)
     y.append(row.col4)

import matplotlib.pyplot as plt
plt.plot(x,y)
plt.ylabel('some numbers')
plt.show()
-----------------


As you can see, in order for me to make the x- and y-vectors, I need to 
make a for-loop to access the individual rows in list_tuples and then I 
append to the x- and y- lists...

Is there any clever way of avoiding this for loop, for either this 
container or another clever container type?

If there isn't, then this is absolutely also an acceptable/good solution 
for me... I also use Matlab and for matrices you can type e.g. plot( 
matrix(:,3), matrix(:,4) ) to plot columns 3 against column 4. But 
Matlab also has this problem, that it cannot store strings and numbers 
in the same matrix - matrices must entirely be numeric, which my data 
isn't (due to 1st column)...

Thanks for any input, if someone has any good ideas...






More information about the Python-list mailing list