0

I tried reading a file with a CSV extension from my local storage but it gave me the following error message.

File "<ipython-input-12-bbfa50761a08>", line 5
    data=pd.read_csv("C:\Users\user\Documents\Projects\Data Science\weather_data.csv")
                    ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

How do I go about this?

Nafis Islam
  • 1,483
  • 1
  • 14
  • 34
  • Does this answer your question? [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python) – Stuart Feb 26 '20 at 03:08
  • Or even more specifically: https://stackoverflow.com/questions/51839136/why-does-pandas-read-csv-have-unicode-error-when-encoding-utf-8-is-specified – AER Feb 26 '20 at 03:08
  • Does this answer your question? [Why does pandas read\_csv have unicode error when encoding utf-8 is specified?](https://stackoverflow.com/questions/51839136/why-does-pandas-read-csv-have-unicode-error-when-encoding-utf-8-is-specified) – AER Feb 26 '20 at 03:09
  • 1
    Sometimes it's hard to know about an answer that exists if the names don't match up. The above however comes up in a Google search. Even the one I posted is a duplicate... – AER Feb 26 '20 at 03:10
  • 1
    `\U` and `\u` has special meaning in string (similar to `\n`, `\t`) - it means Unicode char. So `\Users` and `\user` can be treated as Unicodes (`\UXXXXXXXX`). You can use prefix `r` (raw) and it will not treat `\U`, `\u`, `\n`, `\t` (etc.) as special chars. Or you have to use \\ instead of \ – furas Feb 26 '20 at 03:12
  • Please provide a [mcve]. Have you done any research? – AMC Feb 26 '20 at 03:33

3 Answers3

0

Try shortening your path (see reference) then do:

import numpy as np
import pandas as pd

data = pd.read_csv(r"C:\Users\user\Documents\weather_data.csv")
print(data)

Ref: How to load CSV file in Jupyter Notebook?

whoward3
  • 81
  • 4
0

Please Try:

import numpy as np
import pandas as pd

data=pd.read_csv(r"C:\Users\user\Documents\Projects\Data Science\weather_data.csv")
wwnde
  • 26,119
  • 6
  • 18
  • 32
0

First try loading packages like numpy and pandas and then try loading the data

import numpy as np

import pandas as pd

data=pd.read_csv("C:\Users\user\Documents\Projects\Data Science\weather_data.csv")
print(data)

data.head
L Prathyusha
  • 260
  • 1
  • 3
  • 14