【问题标题】:gspread set_with_dataframe connection timeout. Using OAuth Client IDgspread set_with_dataframe 连接超时。使用 OAuth 客户端 ID
【发布时间】:2021-08-05 20:06:08
【问题描述】:

当我尝试使用 gspreads-dataframes 填写电子表格时,我收到以下错误消息。 我上传的文件有 35000 行,来自 A1:AS35000。 我只找到了不完全适用于我的解决方案,因为所有解决方案都不使用 OAuth 客户端 ID,而是使用服务帐户..

代码如下:

import os
import requests
import gspread
import pandas as pd
import gspread_dataframe as gd


dl_name = FILENAMECSV

spreadsheet_id ='SPREADSHEETID'
gc = gspread.oauth() # Auth to google
sh = gc.open_by_key(spreadsheet_id)   # Open spreadsheet
aux_df_sheet = sh.worksheet('AUX DF')  # Assign worksheet
print('Worksheet connected. Reading in csv and save to pandas.DataFrame')

# pd.read_csv(dtype = 'unicode') check exec time comparison
df = pd.read_csv(f'Fountain_exports/{dl_name}.csv')
df = pd.DataFrame(df)
print('DataFrame saved. Clearing old "AUX DF" sheet content.')
aux_df_sheet.clear()
print('Old sheet content cleared. Updating "AUX DF" with pandas.DataFrame from csv.')


# CLEAR aux_import_sheet SHEET CONTENT
#range_of_cells = aux_df_sheet.range('A1:AS35000')  # -> Select the range you want to clear
#for cell in range_of_cells:
#    cell.value = ''
#    print(f'CLEARING: {cell}')

gd.set_with_dataframe(aux_df_sheet, df)  # APPEND aux_df_sheet TO SHEET CONTENT
print('"AUX DF" updated :)')

这是错误信息:

      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
        httplib_response = self._make_request(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request
        conn.request(method, url, **httplib_request_kw)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 234, in request
        super(HTTPConnection, self).request(method, url, body=body, headers=headers)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1253, in request
        self._send_request(method, url, body, headers, encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1299, in _send_request
        self.endheaders(body, encode_chunked=encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1248, in endheaders
        self._send_output(message_body, encode_chunked=encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1047, in _send_output
        self.send(chunk)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 969, in send
        self.sock.sendall(data)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1204, in sendall
        v = self.send(byte_view[count:])
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1173, in send
        return self._sslobj.write(data)
    socket.timeout: The write operation timed out
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 439, in send
        resp = conn.urlopen(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
        retries = retries.increment(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\util\retry.py", line 532, in increment
        raise six.reraise(type(error), error, _stacktrace)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\packages\six.py", line 769, in reraise
        raise value.with_traceback(tb)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
        httplib_response = self._make_request(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connectionpool.py", line 394, in _make_request
        conn.request(method, url, **httplib_request_kw)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\urllib3\connection.py", line 234, in request
        super(HTTPConnection, self).request(method, url, body=body, headers=headers)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1253, in request
        self._send_request(method, url, body, headers, encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1299, in _send_request
        self.endheaders(body, encode_chunked=encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1248, in endheaders
        self._send_output(message_body, encode_chunked=encode_chunked)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1047, in _send_output
        self.send(chunk)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 969, in send
        self.sock.sendall(data)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1204, in sendall
        v = self.send(byte_view[count:])
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1173, in send
        return self._sslobj.write(data)
    urllib3.exceptions.ProtocolError: ('Connection aborted.', timeout('The write operation timed out'))
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "C:\Users\user\PycharmProjects\FountainAPI\AUXDF.py", line 29, in <module>
        aux_df_sheet.append_rows(data_list)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\models.py", line 1507, in append_rows
        return self.spreadsheet.values_append(range_label, params, body)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\models.py", line 165, in values_append
        r = self.client.request('post', url, params=params, json=body)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\gspread\client.py", line 64, in request
        response = getattr(self.session, method)(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 590, in post
        return self.request('POST', url, data=data, json=json, **kwargs)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\google\auth\transport\requests.py", line 482, in request
        response = super(AuthorizedSession, self).request(
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 542, in request
        resp = self.send(prep, **send_kwargs)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\sessions.py", line 655, in send
        r = adapter.send(request, **kwargs)
      File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\adapters.py", line 498, in send
        raise ConnectionError(err, request=request)
    requests.exceptions.ConnectionError: ('Connection aborted.', timeout('The write operation timed out'))```

【问题讨论】:

    标签: python pandas google-sheets python-requests gspread


    【解决方案1】:

    更新:我找到了一个解决方案,只需将 df 分成两半并将其传输为 2 个块。它以这种方式工作,但我仍然会很高兴有一个更干净的解决方案,可以在一个块中工作。

    感谢您的任何意见:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2018-12-19
      相关资源
      最近更新 更多