Unable to Import csv file into pandas

Asked

Viewed 242 times

-3

Good guys, I’m trying to import a csv file into pandas through the code below:

import pandas as pd

data = pd.read_csv('pop.csv')

He can’t find the file under any circumstances!

Note: The file is in the path of the Macos system: /Users/system/Documents/data

and presents the following error::


Filenotfounderror Traceback (Most recent call last) in -----> 1 data = pd.read_csv('pop.csv')

~/opt/anaconda3/lib/python3.8/site-Packages/pandas/io/parsers.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision) 684 ) 685 --> 686 Return _read(filepath_or_buffer, kwds) 687 688

~/opt/anaconda3/lib/python3.8/site-Packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds) 450 451 # Create the parser. --> 452 parser = Textfilereader(fp_or_buf, **kwds) 453 454 if chunksize or iterator:

~/opt/anaconda3/lib/python3.8/site-Packages/pandas/io/parsers.py in init(self, f, engine, **kwds) 944 self.options["has_index_names"] = kwds["has_index_names"] 945 --> 946 self. _make_engine(self.engine) 947 948 def close(self):

~/opt/anaconda3/lib/python3.8/site-Packages/pandas/io/parsers.py in _make_engine(self, engine) 1176 def _make_engine(self, engine="c"): 1177 if engine == "c": -> 1178 self. _engine = Cparserwrapper(self.f, **self.options) 1179 1180 if engine == "python":

~/opt/anaconda3/lib/python3.8/site-Packages/pandas/io/parsers.py in init(self, src, **kwds) 2006 kwds["usecols"] = self.usecols 2007 -> 2008 self. _Reader = parsers.Textreader(src, **kwds) 2009 self.unnamed_cols = self. _Reader.unnamed_cols 2010

pandas/_libs/parsers.pyx in pandas. _libs.parsers.Textreader.cinit()

pandas/_libs/parsers.pyx in pandas. _libs.parsers.Textreader. _setup_parser_source()

Filenotfounderror: [Errno 2] No such file or directory: 'pop.csv'


I have tried the following solutions:

 data = pd.read_csv('/Users/sistema/Documents/dados/pop.csv')

 data = pd.read_csv('/Users/sistema/Documents/dados/pop.csv', sep = ',', encoding = 'utf-8')

 data = pd.read_csv('C:/Users/sistema/Documents/dados/pop.csv', encoding = 'utf-8')

 data = pd.read_csv(r'C:/Users/sistema/Documents/dados/pop.csv', encoding = 'utf-8')

 data = pd.read_csv('./Users/sistema/Documents/dados/pop.csv')

 data = pd.read_csv('.\Users\sistema\Documents\dados\pop.csv')

>>>>>>>> error desse ultimo com a barra invertida:

File "", line 1 data = pd.read_csv('. Users system Documents data pop.csv') ^ Syntaxerror: (Unicode error) 'unicodeescape' codec can’t Decode bytes in position 1-2: truncated UXXXXXXXX escape


data = pd.read_csv('\\Users\\sistema\\Documents\\dados\\pop.csv')

data = pd.read_csv('.\\Users\\sistema\\Documents\\dados\\pop.csv')

Has anyone experienced this while trying to import csv and managed to solve it? Thank you!!

1 answer

0


If the user is called sistema and the file pop.csv is in the folder /Users/sistema/Documents/dados/, the controls below shall operate:

import pandas as pd
pd.read_csv('~/Documents/dados/pop.csv')

If they don’t work, it’s because the file pop.csv is not in the specified folder.

  • Marcus, I just tested this way and it didn’t work! = ( Today I was able to "solve" by downloading another database called epi.csv. The pop.csv file is in the same folder as the new file I downloaded! I can open epi.csv, but pop.csv does not open for anything! = ( Note: The python file is also in the folder. I will work on the epi.csv folder.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.