importing txt into python script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

importing txt into python script

Kenton Arkill
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
Reply | Threaded
Open this post in threaded view
|

Re: importing txt into python script

Mark Hiner
Hi Kenton,

> Any help appreciated (eg a pointer to the right place to find it)

I'd start looking over similar StackOverflow questions[1,2] and adapting
them to your file structure.

The wiki[3] is a good resource when you're ready to add ImageJ
functionality to your script.

Best,
Mark

[1]
http://stackoverflow.com/questions/3277503/python-read-file-line-by-line-into-array
[2]
http://stackoverflow.com/questions/14676265/how-to-read-text-file-into-a-list-or-array-with-python
[3] http://imagej.net/Jython_Scripting

On Fri, May 15, 2015 at 8: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
Reply | Threaded
Open this post in threaded view
|

Re: importing txt into python script

Kenton Arkill
In reply to this post by Kenton Arkill
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
Reply | Threaded
Open this post in threaded view
|

Re: importing txt into python script

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Kenton Arkill
> On May 15, 2015, at 9: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)

Open the tab-delmited text file as a ResultsTable and then use the getValue() method to retrieve the values. The following JavaScript example opens a two column (“Min” and “Max”) tab-delmited text file as a ResultsTable and retrieves and prints the values.

-wayne

  rt = ResultsTable.open("/Users/wayne/Results.txt");
  for (i=0; i<rt.size(); i++) {
     min = rt.getValue("Min", i);
     max = rt.getValue("Max", i);
     print(i+" "+min+" "+max);
  }



--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: importing txt into python script

bnorthan
In reply to this post by Kenton Arkill
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
Reply | Threaded
Open this post in threaded view
|

Re: importing txt into python script

Kenton Arkill
Thank you all
Brian, I love :from __future__ import with_statement
Regards

On 15 May 2015 at 22:54, Brian Northan <[hidden email]> wrote:

> 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
>

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