Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading .csv file
#1
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.

   

   
Larz60+ write Apr-28-2022, 06:31 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please use BBCode tags for code, not images.
Reply
#2
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.
doug2019 likes this post
Reply
#3
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?
Reply
#4
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'
doug2019 likes this post
Reply
#5
VSCode is an editor. Your problem is your code. Please post some of the things you tried.
doug2019 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 619 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 901 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,094 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 981 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 895 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 984 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,802 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Reading an Input File DaveG 1 1,248 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  Initializing, reading and updating a large JSON file medatib531 0 1,769 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020