Search code examples
pythoncsvfor-loopreadline

Readline function does not print the heders of a CSV file


#Readline function does not print the heders of a CSV file

The CSV file

#Code

 path_TI = 'tips.csv'

 with open(path_TI) as f:

     for i in f:

         print(f.readline())

#Output

16.99,1.01,Female,No,Sun,Dinner,2

21.01,3.5,Male,No,Sun,Dinner,3

24.59,3.61,Female,No,Sun,Dinner,4

8.77,2.0,Male,No,Sun,Dinner,2

...


Solution

  • You're reading from f twice, both when calling f.readline() and in the for loop for i in f

    This causes the program to first read the first line of the file (and setting the variable i to the value) and then printing the next line it reads.

    Your code should probably look like this instead:

    for i in f:
        print(i)