1

I am working on a script that reads through a csv file and returns some information. I am VERY new to python so I may be taking the wrong approach. All I want this part of the script to do is read the file and return the value that is in postion 6 of each row into a list. When I run the function with print, it will print out postion 6 of each row as expected. However, when I change print to return, it only prints position 6 of the first line. I need return to work, because I want to use the result in the next part of the script.

import csv

def gatherQueries():
    queries = []
    with open('AMS.csv') as ams_file:
        csv_reader = csv.reader(ams_file, delimiter=',')
        for row in csv_reader:
            #return row[6]
            print row[6]

gatherQueries()

The code above will show the information I want, but unless I write it to a file and then read that file into the next part of the script (which is cludgy IMO), I have no way of making use of the output. However, when I use return instead of print, I only get the first row:

import csv

def gatherQueries():
    queries = []
    with open('AMS.csv') as ams_file:
        csv_reader = csv.reader(ams_file, delimiter=',')
        for row in csv_reader:
            return row[6]
            #print row[6]

search = gatherQueries()
print search
2
  • 2
    you can append the rows to queries list, and then return queries outside the for loop. Commented Jun 17, 2021 at 5:02
  • Not related to the problem, but nevertheless important: According to your usage of print with a statement in print search you seem to use Python 2. Don't do that. Python 2 is dead since the beginning of last year and that was announced over 10 years ago. Switch to Python 3. Now. Commented Jun 17, 2021 at 6:06

3 Answers 3

2

You almost had it.

import csv

def gatherQueries():
    queries = []
    with open('AMS.csv') as ams_file:
        csv_reader = csv.reader(ams_file, delimiter=',')
        for row in csv_reader:
            queries.append( row[6] )
    return queries

search = gatherQueries()
print search
Sign up to request clarification or add additional context in comments.

1 Comment

The above is needed because when you use "return" in your original code, it returns out of the function the first time that line is run in the loop. The function exits and there is no loop execution. So appending to a list keeps the loop running.
0

You need to append results to the list and return the list

def gatherQueries():
    queries = []
    with open('AMS.csv') as ams_file:
        csv_reader = csv.reader(ams_file, delimiter=',')
        for row in csv_reader:
            queries.append(row[6])
    return queries
    
search = gatherQueries()
print(search)

Comments

0

Try this

import csv

def gatherQueries():
    queries = []
    with open('AMS.csv') as ams_file:
        csv_reader = csv.reader(ams_file, delimiter=',')
        for row in csv_reader:
            queries.append(row[6])
    return queries 

search = gatherQueries()
print search

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.