Home > Software design >  Python Read In Google Spreadsheet Using Pandas
Python Read In Google Spreadsheet Using Pandas

Time:08-05

I have file in Google sheets I want to read it into a Pandas Dataframe. But gives me an error i don't know what's it. this is the code :

import pandas as pd
sheet_id = "1HUbEhsYnLxJP1IisFcSKtHTYlFj_hHe5v21qL9CVyak"
df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export? 
gid=556844753&format=csv")
print(df)

And this is the error :

File "c:\Users\blaghzao\Documents\Stage PFA(Laghzaoui Brahim)\google_sheet.py", line 3, in df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?gid=556844753&format=csv") File "C:\Users\blaghzao\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util_decorators.py", line 311, in wrapper return func(*args, **kwargs) File "C:\Users\blaghzao\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv return _read(filepath_or_buffer, kwds) File "C:\Users\blaghzao\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read return parser.read(nrows) File "C:\Users\blaghzao\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1255, in read index, columns, col_dict = self._engine.read(nrows) File "C:\Users\blaghzao\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read chunks = self._reader.read_low_memory(nrows) File "pandas_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory File "pandas_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows File "pandas_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows File "pandas_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error pandas.errors.ParserError: Error tokenizing data. C error: Expected 89 fields in line 3, saw 243

CodePudding user response:

I found the answer, the problem it's just with access permissions of the file.

enter image description here

CodePudding user response:

Remove gid from code

import pandas as pd
sheet_id = "1HUbEhsYnLxJP1IisFcSKtHTYlFj_hHe5v21qL9CVyak"
df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")
print(df)

Click on link for sample image

CodePudding user response:

As far as I know this error rises using comma delimiter and you have more commas then expected.

Can you try with below read_csv() method to avoid them;

df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export? gid=556844753&format=csv", on_bad_lines='skip')

This will avoid bad lines so you can identify problem depending on skipped lines. I believe your csv format export is not matching with what pandas read_csv() expects.

  • Related