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


Groups > comp.lang.python > #41544

CSV, lists, and functions

Newsgroups comp.lang.python
Date 2013-03-19 15:59 -0700
Message-ID <1d7fcebe-8677-42ec-a53d-284214296d37@googlegroups.com> (permalink)
Subject CSV, lists, and functions
From "C.T." <swilks06@gmail.com>

Show all headers | View raw


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.

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


Thread

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

csiph-web