[Tutor] CSV question for getting data from a text file

Ara Kooser ghashsnaga at gmail.com
Mon Oct 19 02:36:26 CEST 2009


Hello all,

  I fell off the python programming wagon for awhile and I am quite rusty. I
a text file that looks like this

*a bunch of stuff above*
    O(-2)/O(0)          12.5731      0.7181

----------------------------Distribution of
species----------------------------

                                               Log       Log         Log
    Species            Molality    Activity  Molality  Activity     Gamma

    OH-               4.121e-06   3.489e-06    -5.385    -5.457    -0.072
    H+                1.437e-09   1.259e-09    -8.843    -8.900    -0.057
    H2O               5.551e+01   9.994e-01     1.744    -0.000     0.000
C(4)             5.334e-03
    HCO3-             4.517e-03   3.877e-03    -2.345    -2.411    -0.066
*stuff below*

I wrote this program:

import string
import csv, sys
import matplotlib.pyplot as plt
import numpy as np


filein = raw_input("What is the name of your file?")
testreader = csv.reader(open(filein))
for row in testreader:
    print(row)

The output looks like this:
[]
['----------------------------Distribution of
species----------------------------']
[]
['\t                                           Log       Log         Log ']
['\tSpecies            Molality    Activity  Molality  Activity     Gamma']
[]
['\tOH-               4.121e-06   3.489e-06    -5.385    -5.457    -0.072']
['\tH+                1.437e-09   1.259e-09    -8.843    -8.900    -0.057']
['\tH2O               5.551e+01   9.994e-01     1.744    -0.000     0.000']

What I am interested in is the species column and the activity column. The
eventual goal is do do a bunch of calculations on the activties and plot it
up using matplotlib.

How do ignore everything before and after the Distribution of species and
then pull out the species and activties so I can perform calculations on
them? Is csv the best for that or am I better off using split and dumping
into a dictionary? I think I need to specify white space as the delimiter,
is that correct?

Thank you very much.

Ara

-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub
cardine glacialis ursae.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091018/2cb86b4e/attachment.htm>


More information about the Tutor mailing list