-1

i am trying to put the iris data set into a list however when i try to read the lines in the file it doesn't separate each value and instead puts it in 1 string. how do i fix this so that it opens and separate the data? some of the data:

sepal_length,sepal_width,petal_length,petal_width,species
5.1,3.5,1.4,0.2,setosa
4.9,3.0,1.4,0.2,setosa
4.7,3.2,1.3,0.2,setosa
4.6,3.1,1.5,0.2,setosa
5.0,3.6,1.4,0.2,setosa
5.4,3.9,1.7,0.4,setosa
4.6,3.4,1.4,0.3,setosa

code:

import csv

with open("iris.csv", "r") as csv_iris:
    read = csv.reader(csv_iris, delimiter = ",")

    for line in csv_iris:
        print(line[0])

output:

s
5
4
4
4
5
5
4

what output should be:

sepal_length
5.1
4.9
4.7
4.6
5.0
5.4
cs95
  • 379,657
  • 97
  • 704
  • 746
Regi
  • 31
  • 1
  • 6

2 Answers2

1

You were iterating over the file rather than the csv reader. That caused the first character of each line to be printed.

import csv

with open("iris.csv", "r") as csv_iris:
    read = csv.reader(csv_iris, delimiter = ",")    
    for line in read:
        print(line[0])
cs95
  • 379,657
  • 97
  • 704
  • 746
adm_
  • 642
  • 6
  • 13
0

I commented the mistake

with open('iris.csv','r') as csv_iris:
    read = csv.reader(csv_iris,delimiter=',')
#Instead of calling for read, you called for csv_iris
    for line in read:
        print(line[0])
Andy K
  • 4,944
  • 10
  • 53
  • 82