Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'lines,': 0.05; 'suppose': 0.07; 'try:': 0.07; 'int)': 0.09; 'ioerror:': 0.09; 'def': 0.10; 'index': 0.13; "'r')": 0.16; 'beginning.': 0.16; 'columns': 0.16; 'csv': 0.16; 'f.seek(0)': 0.16; 'file_name': 0.16; 'ignores': 0.16; 'reposition': 0.16; 'subject:CSV': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'skip:v 30': 0.20; 'file:': 0.22; "i've": 0.23; 'least': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'values': 0.26; 'appreciated.': 0.26; 'module.': 0.27; 'object,': 0.27; 'lines': 0.28; 'once,': 0.29; 'class': 0.29; "i'm": 0.29; 'function': 0.30; 'file': 0.32; 'subject:lists': 0.32; 'switch': 0.32; 'print': 0.32; 'extract': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'project': 0.34; 'list': 0.35; 'lists.': 0.35; 'doing': 0.35; 'pm,': 0.35; 'list.': 0.35; 'created': 0.36; 'except': 0.36; 'but': 0.36; 'closing': 0.36; 'should': 0.36; 'turn': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'hello,': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'think': 0.40; 'subject:, ': 0.61; 'first': 0.61; 'back': 0.62; 'information': 0.63; 'here': 0.65; 'six': 0.65; 'received:74.208': 0.71; 'prompt': 0.78; 'c.t.': 0.84; 'column.': 0.84; 'from.': 0.93 Date: Tue, 19 Mar 2013 21:29:41 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: CSV, lists, and functions References: <1d7fcebe-8677-42ec-a53d-284214296d37@googlegroups.com> In-Reply-To: <1d7fcebe-8677-42ec-a53d-284214296d37@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:/s2C8y/OWE1AEmzmqQyp4x1uo1njmaz/pfmd45e7INw AkXI1NFudZFUJ6OmNb1Ab+URjIDoCsjP3XilqQKgr51+6ELQFm K6n+YqHiEOkdBQpAcZTvDyL0RWKvnft3vr/LR22a2DZ+uXS7fb eB0j7UXsmZbUgydLMxXHxtpzTMcMKJkjuFdBC/ekVoYMgXQALA It4MEgyg9eymFlHcgH3n3TD5tN8P9jumtnTsDitAbPE1VEpG6t gyDBGqkrAsiX8khefrVOpd8cWFpPB877s6b8SnzhVK+3L3nPQ4 iLLr7pRSy8pfG96kv9SpZHyL8jxkZ5Nf2lBDDm2uTa0ADQYrw= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363742997 news.xs4all.nl 6853 [2001:888:2000:d::a6]:49730 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41552 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