1

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
1
  • 1
    The URL you are using points to a nicely formatted table embedded in HTML. Look at s. It contains much more than the csv data. Commented Mar 3, 2022 at 15:43

1 Answer 1

3

Use the url for the raw data.

pd.read_csv('https://raw.githubusercontent.com/kjhealy/fips-codes/master/state_and_county_fips_master.csv')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.