![]() |
Reading .csv file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Reading .csv file (/thread-37068.html) |
Reading .csv file - doug2019 - Apr-28-2022 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] RE: Reading .csv file - deanhystad - Apr-28-2022 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) Notice that there is only 1 column. You'll need to specify the separator is ";" to get two columns.
RE: Reading .csv file - doug2019 - Apr-29-2022 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? RE: Reading .csv file - snippsat - Apr-29-2022 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 escapeCan 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' RE: Reading .csv file - deanhystad - Apr-29-2022 VSCode is an editor. Your problem is your code. Please post some of the things you tried. |