##### Helps with reading csv file - 3 methods
 Helps with reading csv file - 3 methods hhchenfx Programmer named Tim Posts: 6 Threads: 3 Joined: May 2021 Reputation: May-13-2021, 01:37 AM Hi Everyone, I try 3 different ways to read csv data and only 1 works. Can you please help to fix the not working code? Thank you so much. HHC #Working import pandas as pd df1 = pd.read_csv (r'C:\temp\text.csv') print(df1) #Not working with open("C:\temp\text.csv", "r") as text: print(text) #Not working f = open("C:\temp\text.csv", "r") print(f.read()) Reply bowlofred Da Bishop Posts: 1,270 Threads: 3 Joined: Mar 2020 Reputation: May-13-2021, 02:07 AM What does "not working" mean? What do you expect it to do? What does it do instead? Do you get error messages? What is the output? The second two should just show you the content of file. Reply Larz60+ aetate et sapientia Posts: 10,666 Threads: 400 Joined: Sep 2016 Reputation: May-13-2021, 02:42 AM One additional note to add to bowlofred's list. Please include a small sample of the CSV data. Reply hhchenfx Programmer named Tim Posts: 6 Threads: 3 Joined: May 2021 Reputation: May-13-2021, 02:59 AM Hi, The text.csv has 2 columns: id info 1, From: abc to xyz 2, From abc yxz I want to print the data out. The second code error is: with open("C:\temp\text.csv", "r") as text: OSError: [Errno 22] Invalid argument: 'C:\temp\text.csv' The third code error is: f = open("C:\temp\text.csv", "r") OSError: [Errno 22] Invalid argument: 'C:\temp\text.csv' Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: May-13-2021, 04:15 AM note the escape sequence \t in the file name. use raw string like in the first snippet, escape the back-slash, e.g. \\ or use forward slash. Also, in the second snippet you want to print(text.read()). Instead you can iterate over file and print line by line If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply