Home > OS >  Pandas Error attempting to read csv from a url
Pandas Error attempting to read csv from a url

Time:03-04

I am trying to read a csv file from a URL and have tried a couple different methods. I get an error when reading the file.

import io
url="https://github.com/kjhealy/fips-codes/blob/master/state_and_county_fips_master.csv"
s=requests.get(url).content
c=pd.read_csv(io.StringIO(s.decode('utf-8')))

countyFIPS = pd.read_csv('https://github.com/kjhealy/fips-codes/blob/master/state_and_county_fips_master.csv')

Traceback:

    /Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/io/parsers/c_parser_wrapper.py in read(self, nrows)
    221         try:
    222             if self.low_memory:
--> 223                 chunks = self._reader.read_low_memory(nrows)
    224                 # destructive to chunks
    225                 data = _concatenate_chunks(chunks)

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.read_low_memory()

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._read_rows()

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._tokenize_rows()

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/_libs/parsers.pyx in pandas._libs.parsers.raise_parser_error()

ParserError: Error tokenizing data. C error: Expected 1 fields in line 26, saw 376

CodePudding user response:

Use the url for the raw data.

pd.read_csv('https://raw.githubusercontent.com/kjhealy/fips-codes/master/state_and_county_fips_master.csv')
  • Related