简体   繁体   中英

how to make python print the whole file instead of just the single line

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM