Problems with Reading a csv file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problems with Reading a csv file

Hi i hope there is someone who can help me figure out what it is i am doing wrong when i try to read and view the data from a csv file i get the following error File "file0.py", line 2 def open_csv_file (".. \ Playground \\ Python \ Pythontest \ CSV files \ numbers.csv"): ^ SyntaxError: invalid syntax

26th Jun 2022, 11:39 AM
Benny
Benny  - avatar
5 Answers
+ 2
do not use a simple \ in the path, use two of them: "..\\Playground\\Python..." also, make sure to removes spaces in the path as it will not find the file. I noticed you are placing a string as a parameter definition, that will not work, do this instead: def open_csv_file(path): # code to open the file here open_csv_file("..\\path\\to\\file.csv") ------------- side note: don't put url in the tags, it's unclickable there. you could use "python" and "syntax error" in there, it'd be more relevent.
26th Jun 2022, 12:26 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
Hi Apollo thanks for your input but I'm afraid I do not understand it correctly as the code still does not work for me thanks for your side note, I will remember that for next time
2nd Jul 2022, 2:28 PM
Benny
Benny  - avatar
0
Are you getting new errors? What does your code looks like now?
2nd Jul 2022, 7:55 PM
Apollo-Roboto
Apollo-Roboto - avatar
0
I have changed my code to the following. import pandas as pd def open_csv_file ("\\ Users \\ krumh \\ Documents \\ BKLalt \\ Python \\ Pythontest \\ CSVfiler \\ numbers.csv"): open_csv_file ("\\ Users \\ krumh \\ Documents \\ BKLalt \\ Python \\ Pythontest \\ CSVfiler \\ numbers.csv")
2nd Jul 2022, 8:39 PM
Benny
Benny  - avatar
0
I'd recommend you to have another look at the function lessons. You cannot delare a string in the parameters of a function. look at my example more carefully in my previous answer. Also, you have changed the path to use double \\ but you left the spaces, which makes it an invalid path. "\\ Users \\ krumh \\ Documents \\ BKLalt \\ Python \\ Pythontest \\ CSVfiler \\ numbers.csv" vs "\\Users\\krumh\\Documents\\BKLalt\\Python\\Pythontest\\CSVfiler\\numbers.csv"
3rd Jul 2022, 8:06 PM
Apollo-Roboto
Apollo-Roboto - avatar