-1

I am pretty new to python (or programming in general). Currently i am trying to open a CSV file.

with open("C:/Users/Sascha/Desktop/Kursmaterialien/Kursmaterialien/data/names.csv", "r") as file: 
    for line in file:
        print(line)
        break

For some reason python does not give me any responds (Not even a Traceback). That happend to me earlier with another file aswell and i dont now what the problem is.... Sooo, what should i do?

sasi
  • 1
  • 1
  • 1
    It doesn't work like that, your code does the same as opening a csv file in notepad, which is nothing but bunch of junk you wont understand, you need some special libraries to read csv files in python you can use the csv or pandas library , you can learn how to do so here https://realpython.com/python-csv/. – JaniniRami Aug 13 '20 at 20:42
  • 3
    Does this answer your question? [Reading rows from a CSV file in Python](https://stackoverflow.com/questions/13428318/reading-rows-from-a-csv-file-in-python) – DV82XL Aug 14 '20 at 00:32

3 Answers3

0

There are multiple problems that could raise the problem mentioned.

The first is that you have the wrong iterator. For reading lines file.readlines() is the appropriate function. To read the csv file as csv you can take advantage of python's csv lib.

If you try to just read the first line use file.readline(). in your above mentioned code you use break after the first line. break ends the loop and in your case in the first iteration.

More informations on csvs with python guru99

More information about working with files in python realpython.com

For more help providing a MRE for your problem.

Cobalt
  • 447
  • 5
  • 9
0

are you importing csv?

import csv 
with open("file.csv", "r") as file: 
for line in file:
    print(line)
    break
Cristiano
  • 1
  • 1
0

How to load csv dataset and print

import pandas as pd
data = pd.read_csv('C:/Users/admin/Music/Project/AMia-1981-2021.csv')
print(data)
Developer-Felix
  • 337
  • 2
  • 4