Login  Register

Re: importing txt into python script

Posted by bnorthan on May 15, 2015; 9:54pm
URL: http://imagej.273.s1.nabble.com/importing-txt-into-python-script-tp5012843p5012849.html

Hi Kenton

There is a csv reader in python that is also available in jython.   The
below jython script shows how to use the csv reader (adapted from here
https://docs.python.org/2/library/csv.html), as well as a jython version of
Wayne's example (my example assumes there are columns named 'Area' and
'Intensity' in the file. )

from __future__ import with_statement
from ij.measure import ResultsTable
import csv

filename='yourfile.csv'

# open using csv
with open(filename, 'rb') as csvfile:
 thereader = csv.reader(csvfile, delimiter=',')
 for row in thereader:
  print row

#open using ResultsTable
rt = ResultsTable.open(filename)

for i in range(rt.size()):
 area = rt.getValue('Area', i);
 intensity = rt.getValue('Intensity', i);
 print str(area)+" "+str(intensity);

On Fri, May 15, 2015 at 2:00 PM, Kenton Arkill <[hidden email]>
wrote:

> Thanks for helping:
>
> However in Fiji it uses Jython which has the following statement! (found in
> [3] from Mark)
>
>
>    - *Some existing python modules can't be imported in jython.*
>
> This is for instance the case of the module *numpy*, which would have been
> really convenient for analysing data and results.
> So anyway I'm playing with which at least works for a single column text
> list with no headings:
> Filename = "/Users/kparkill/Desktop/test.txt"
> f= open(Filename, 'r')
> g=f.readlines()
> a= []
> for line in g:
>     a.append(float(line))
>     print line
> print a
>
> On 15 May 2015 at 17:30, Zack Gainsforth <[hidden email]> wrote:
>
> > import numpy as np
> >
> > Filename = ‘whatever your file is.txt’
> >
> > # Create an array of numbers from the text data in the file.  skip_header
> > skips over the header row which will not be numbers.
> > Nums = np.genfromtxt(Filename, skip_header=1)
> >
> > print Nums
> >
> > Hope that gets you started!
> >
> > Zack
> >
> > On May 15, 2015, at 6:08 AM, Kenton Arkill <[hidden email]>
> > wrote:
> >
> > Hi
> > I'm a newby at python/jython. I wish to import txt files and make them
> > lists of numbers so I can do things with them. my txt files are 2 columns
> > and lots of rows (with header name) and tab delimited.
> > Any help appreciated (eg a pointer to the right place to find it)
> > Regards
> > Kenton
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
> >
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html