0

I need to extract column from a table and compare each element of this column with number 5. I am getting a list of element from the column but it shows an empty output in console.

def compare():
    with open('table.csv') as csv_file:
        lines = csv_file.readlines()
        for line in lines[1::]:
            array = line.split(',')
            list_pk = array[1]
        if list_pk == "5":
            print("Match")
1
  • where do you get error and what error is it? Can you provide an example of file input? Commented Sep 27, 2019 at 13:47

1 Answer 1

1

At a first glance of your code, your if statement should be inside the for loop for it to print anything unless you get lucky and last line has a match:

def compare():
    with open('table.csv') as csv_file:
        lines = csv_file.readlines()
        for line in lines[1::]:
            array = line.split(',')
            list_pk = array[1]
            if list_pk == 5:
                print("Match")
Sign up to request clarification or add additional context in comments.

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.