Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77858

Re: CSV methodology

Date 2014-09-14 02:51 +0000
From kjs <bfb@riseup.net>
Subject Re: CSV methodology
References <b2q91ada6b59ept81ac65vtnnu6sdklp1h@4ax.com>
Newsgroups comp.lang.python
Message-ID <mailman.14004.1410663113.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

jetrn@newsguy.com wrote:
> 
> Hello.  Back in the '80s, I wrote a fractal generator, which, over the years,
> I've modified/etc to run under Windows.  I've been an Assembly Language
> programmer for decades.  Recently, I decided to learn a new language,
> and decided on Python, and I just love it, and the various IDEs.
> 
> Anyway, something I thought would be interesting, would be to export
> some data from my fractal program (I call it MXP), and write something
> in Python and its various scientific data analysis and plotting modules,
> and... well, see what's in there.
> 
> An example of the data:
> 1.850358651774470E-0002
> 32
> 22
> 27
> ... (this format repeats)
> 
> So, I wrote a procedure in MXP which converts "the data" and exports
> a csv file.  So far, here's what I've started with:
> 
> -----------------------------------------------
> import csv
> 
> fname = 'E:/Users/jayte/Documents/Python Scripts/XportTestBlock.csv'
> 
> f = open(fname)
> 
> reader = csv.reader(f)
> 
> for flt in reader:
>     x = len(flt)
> file.close(f)
> -----------------------------------------------


The csv.reader(f) object creates an iterable that will create lists from
lines in f. The list will have values at indexes based on the commas in
the file. EX:

my_header_1, my_header_2
111, 0001
101, 1010
100, 1001

The csv.reader will lazily make lists like ['my_header_1',
'my_header_2'], ['111', '0001'], ... and so forth.

Your program above will take the length of those lists, and assign that
value to x. For every line in the file, f will get rewritten with a new
value, the length of list which is derived from the number of commas in
the csv.

Also note that the csv.reader speaks many dialects, and can do similar
work on files with different quote characters and delimiters.

Generally, I prefer to read in csv files with the standard readline()
method of open files. I do like to use csv.DictWriter, which helps me to
keep my csv output tabular.

-Kevin

> 
> This will get me an addressable array, as:
> 
> flt[0], flt[1], flt[350], etc...  from which values can be assigned to
> other variables, converted...
> 
> My question:  Is there a better way?  Do I need to learn more about
> how csv file are organized?  Perhaps I know far too little of Python
> to be attempting something like this, just yet.
> 
> Advice?
> 
> Jeff
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

CSV methodology jetrn@newsguy.com - 2014-09-13 21:34 -0400
  Re: CSV methodology kjs <bfb@riseup.net> - 2014-09-14 02:51 +0000
  Re: CSV methodology Terry Reedy <tjreedy@udel.edu> - 2014-09-14 03:02 -0400
    Re: CSV methodology jayte <jetrn@newsguy.com> - 2014-09-14 12:56 -0400
      Re: CSV methodology Chris Angelico <rosuav@gmail.com> - 2014-09-15 03:10 +1000
      Re: CSV methodology Terry Reedy <tjreedy@udel.edu> - 2014-09-14 14:42 -0400
        Re: CSV methodology jayte <jetrn@newsguy.com> - 2014-09-14 16:19 -0400
      Re: CSV methodology Peter Otten <__peter__@web.de> - 2014-09-15 09:29 +0200
        Re: CSV methodology jayte <jetrn@newsguy.com> - 2014-09-15 12:33 -0400
          Re: CSV methodology Peter Otten <__peter__@web.de> - 2014-09-16 13:22 +0200
            Re: CSV methodology jayte <jetrn@newsguy.com> - 2014-09-16 14:03 -0400
            Works perfectly (was Re: CSV methodology) jayte <jetrn@newsguy.com> - 2014-09-22 20:27 -0400
              Re: Works perfectly (was Re: CSV methodology) Peter Otten <__peter__@web.de> - 2014-09-23 09:59 +0200
  Re: CSV methodology Cameron Simpson <cs@zip.com.au> - 2014-09-14 18:38 +1000
    Re: CSV methodology Rustom Mody <rustompmody@gmail.com> - 2014-09-14 01:56 -0700
      Re: CSV methodology Cameron Simpson <cs@zip.com.au> - 2014-09-15 09:28 +1000
  Re: CSV methodology Akira Li <4kir4.1i@gmail.com> - 2014-09-15 11:12 +0400
    Re: CSV methodology pH <high@cidity.level> - 2014-09-15 12:40 -0400
  Re:CSV methodology Dave Angel <davea@davea.name> - 2014-09-15 09:29 -0400
    Re: CSV methodology jayte <jetrn@newsguy.com> - 2014-09-15 12:53 -0400

csiph-web