-1

I have a .csv file that has just one column of numbers and I want to read each number in the column and print it in the console like this:

1
2
3
4

here is the code that I have used:

file_reference2 = open("file1.csv", "r")
read_lines1 = file_reference1.readlines()
for line1 in read_lines1:
    print(line1)

file_reference1.close()

what I expect is:

1
2
3

in the console.

But what I get is:

1 

And the program stops. How do I make it print the whole file?

blhsing
  • 91,368
  • 6
  • 71
  • 106
Connel Chen
  • 69
  • 1
  • 1
  • 3
    Where's your `file_reference1` defined? Or is that a typo? – blhsing Apr 02 '19 at 18:14
  • Why do you not expect it to print `4`? – mkrieger1 Apr 02 '19 at 18:17
  • Cannot reproduce. It seems like a typo since your assigning `open` to `file_reference2`, but you're using `file_reference1`. – Vasilis G. Apr 02 '19 at 18:18
  • Things I usually do when posting about parsing a file are; 0) narrow the problem down to the smallest amount of code and data where the issue is still reproducible, 1) Python version, `python --version` 2) show all or part of file, `cat /path/to/file` 3) show all of code, `cat /path/to/script.py`, 4) show output of running the code on the CLI. This will make it easier to understand exactly what is happening. – Tim Apr 02 '19 at 18:47

1 Answers1

0

You create a variable file_reference2, but later call file_reference1.readlines() (notice the difference in variable names). You are probably reading lines from a wrong file as this code works well for me if I change that line to file_reference2.readlines() like this:

file_reference2 = open("file1.csv", "r") 
read_lines1 = file_reference2.readlines() 
for line1 in read_lines1: 
    print(line1)

file_reference2.close()
Andry
  • 339
  • 7
  • 23