0

I am trying to loop through a .csv file so I can read each line, but I am receiving the following error:

AttributeError: '_io.TextIOWrapper' object has no attribute 'iterrows'

Here is my code so far:

def i_list(input_file):

    input_list = []
    file = open(input_file, 'r')
    for i, j in file.iterrows():
            num1 = j[1]
            num2 = j[2]
            input_list.append(num1)
            input_list.append(num2)

i_list('nums.csv')
2

2 Answers 2

2

You can use the csv library

import reader from csv

input_list=[]
f = reader(open(input_file))
header = next(f) # if there is a header
for num1, num2 in f:
    input_list.append(num1)
    input_list.append(num2)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Pandas:

import pandas as pd
df = pd.read_csv('file.csv')
for row in df.iterrows():
  # do something

If you don't want to use Pandas, try to read the file using: file.readlines()

with open(filepath, 'r') as f:
  lines = f.readlines()

for line in lines:
  # do something

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.