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


Groups > comp.lang.python > #41544 > unrolled thread

CSV, lists, and functions

Started by"C.T." <swilks06@gmail.com>
First post2013-03-19 15:59 -0700
Last post2013-03-20 01:11 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  CSV, lists, and functions "C.T." <swilks06@gmail.com> - 2013-03-19 15:59 -0700
    Re: CSV, lists, and functions Roy Smith <roy@panix.com> - 2013-03-19 19:19 -0400
    Re: CSV, lists, and functions Dave Angel <davea@davea.name> - 2013-03-19 21:29 -0400
      Re: CSV, lists, and functions Roy Smith <roy@panix.com> - 2013-03-19 21:37 -0400
        Re: CSV, lists, and functions rusi <rustompmody@gmail.com> - 2013-03-20 01:11 -0700

#41544 — CSV, lists, and functions

From"C.T." <swilks06@gmail.com>
Date2013-03-19 15:59 -0700
SubjectCSV, lists, and functions
Message-ID<1d7fcebe-8677-42ec-a53d-284214296d37@googlegroups.com>
Hello,

Currently doing a project for class an I'm stuck. I have a csv file that I'm suppose to extract some information from. I've created a function that ignores the first six lines of the csv file and creates a list of values in a particular column. Here is the code:


def get_values(file, index):

    '''(file object, int) -> list
    Return a list of states and corresponding values at a prticular index in file.'''
    
    values_list = [] 
    file.readlines(900)#skipping first 6 lines of file
    for line in file:
        line_list = line.split(',')
        values_list.append(line_list[index])
    
    return values_list
    


#Prompt for file and prompt again if file is not found
while True:
    try:
        file_name = input('Enter in file name: ')
        input_file = open( file_name, 'r')
        break

    except IOError:
         print('File not found.')

heart_list = get_values(input_file, 1)
motor_list = get_values(input_file, 4)

I can see a list of values for heart_list, but when I print the motor_list I get back an empty list. I think its because I'm not closing the file before calling the function again, but I don't want to prompt for the same file over and over again because I need to pull at least 10 columns from this csv files and turn them into lists. Any help is appreciated.

[toc] | [next] | [standalone]


#41546

FromRoy Smith <roy@panix.com>
Date2013-03-19 19:19 -0400
Message-ID<roy-DED2D3.19190719032013@70-1-84-166.pools.spcsdns.net>
In reply to#41544
In article <1d7fcebe-8677-42ec-a53d-284214296d37@googlegroups.com>,
 "C.T." <swilks06@gmail.com> wrote:

> Currently doing a project for class an I'm stuck. I have a csv file that I'm 
> suppose to extract some information from. I've created a function that 
> ignores the first six lines of the csv file and creates a list of values in a 
> particular column. Here is the code:
> 
> 
> def get_values(file, index):
> 
>     '''(file object, int) -> list
>     Return a list of states and corresponding values at a prticular index in 
>     file.'''
>     
>     values_list = [] 
>     file.readlines(900)#skipping first 6 lines of file

Your intent is to skip the first 6 lines, but your code says "skip the 
first 900 characters, plus enough to get to the end of the next line".  
You're assuming 900 characters gets you to line 6.  That's really 
fragile.

If you really want to skip the first 6 lines, try something like:

for i in range(6):
   file.readline()

The next problem I see is:

    for line in file:
        line_list = line.split(',')

You don't want to do that.  Use the csv module 
(http://docs.python.org/2/library/csv.html) to parse the file.  It 
handles quotes, embedded commas, and all sorts of annoying edge cases 
you've probably never thought of.

[toc] | [prev] | [next] | [standalone]


#41552

FromDave Angel <davea@davea.name>
Date2013-03-19 21:29 -0400
Message-ID<mailman.3540.1363742997.2939.python-list@python.org>
In reply to#41544
On 03/19/2013 06:59 PM, C.T. wrote:
> Hello,
>
> Currently doing a project for class an I'm stuck. I have a csv file that I'm suppose to extract some information from. I've created a function that ignores the first six lines of the csv file and creates a list of values in a particular column. Here is the code:
>
>
> def get_values(file, index):
>
>      '''(file object, int) -> list
>      Return a list of states and corresponding values at a prticular index in file.'''
>
>      values_list = []
>      file.readlines(900)#skipping first 6 lines of file
>      for line in file:
>          line_list = line.split(',')
>          values_list.append(line_list[index])
>
>      return values_list
>
>
>
> #Prompt for file and prompt again if file is not found
> while True:
>      try:
>          file_name = input('Enter in file name: ')
>          input_file = open( file_name, 'r')
>          break
>
>      except IOError:
>           print('File not found.')
>
> heart_list = get_values(input_file, 1)
> motor_list = get_values(input_file, 4)
>
> I can see a list of values for heart_list, but when I print the motor_list I get back an empty list. I think its because I'm not closing the file before calling the function again, but I don't want to prompt for the same file over and over again because I need to pull at least 10 columns from this csv files and turn them into lists. Any help is appreciated.
>

f.seek(0)

will reposition the file to the beginning.

But you should switch to using the csv module.  And unless you have data 
that consists of millions of lines, you should just read the whole thing 
in once, and then extract the various columns by simple list 
manipulations and/or comprehensions.


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#41553

FromRoy Smith <roy@panix.com>
Date2013-03-19 21:37 -0400
Message-ID<roy-063127.21373419032013@70-1-84-166.pools.spcsdns.net>
In reply to#41552
In article <mailman.3540.1363742997.2939.python-list@python.org>,
 Dave Angel <davea@davea.name> wrote:

> But you should switch to using the csv module.  And unless you have data 
> that consists of millions of lines, you should just read the whole thing 
> in once, and then extract the various columns by simple list 
> manipulations and/or comprehensions.

Another possibility is to use pandas (http://pandas.pydata.org/).  It's 
a bit of effort to master the package, but it's got some neat stuff in 
it.  Apropos to this thread, it has a very flexible read_csv() function.  
You can pass it, for example, skiprows=6, and it'll, well, skip the 
first 6 rows.

[toc] | [prev] | [next] | [standalone]


#41575

Fromrusi <rustompmody@gmail.com>
Date2013-03-20 01:11 -0700
Message-ID<4cd9f839-0ddf-4069-b753-7f2c92dde59e@vv8g2000pbc.googlegroups.com>
In reply to#41553
On Mar 20, 6:37 am, Roy Smith <r...@panix.com> wrote:
>
> Another possibility is to use pandas (http://pandas.pydata.org/).

Thanks for the link -- looks interesting!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web