Python Forum

Full Version: Reading .csv file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! Could someone give me a hand? I'm having trouble creating code to read a .csv file in pandas. With .xlsx file I know how to create it. Below is the print of the script and csv example.

[attachment=1728]

[attachment=1729]
Backslash is the start of an escape character. You can use "\\", use a "raw" string for the filename, or better yet, not specify the file path at all.
from io import StringIO
import pandas as pd

stringdata = StringIO("""
Start Date;End Date
2019-10-01;2019-10-05
2019-12-15;2020-01-05
2020-02-20;2020-02-25
""")

# Change comment to use string dta or data file
#df = pd.read_csv(stringdata)
df = pd.read_csv("data.csv")
print(df)
Output:
Start Date;End Date 0 2019-10-01;2019-10-05 1 2019-12-15;2020-01-05 2 2020-02-20;2020-02-25
Notice that there is only 1 column. You'll need to specify the separator is ";" to get two columns.
Nothing worked. I tried with import csv, I put r before the path file, \\ as a separator, encoding: "utf-8" and sep: ";", but it didn't work. Could it be some VS Code configuration?
The error in your image is clear,can make the error as a example.
>>> path = 'C:\Users'
  File "<interactive input>", line 1
    path = 'C:\Users'
                     ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Can not use \ in path because escape character(problem here is \U).
So to fix all this works.
>>> path = r'C:\Users'
>>> path = 'C:\\Users'
>>> path = 'C:/Users'
VSCode is an editor. Your problem is your code. Please post some of the things you tried.