gspread set_with_dataframe连接超时.使用OAuth客户端ID



当我尝试用gspread- dataframe填充电子表格时,我得到下面的错误消息。我上传的文件有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:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1253, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1299, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1248, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1047, in _send_output
self.send(chunk)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 969, in send
self.sock.sendall(data)
File "C:UsersuserAppDataLocalProgramsPythonPython39libssl.py", line 1204, in sendall
v = self.send(byte_view[count:])
File "C:UsersuserAppDataLocalProgramsPythonPython39libssl.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:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesrequestsadapters.py", line 439, in send
resp = conn.urlopen(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3utilretry.py", line 532, in increment
raise six.reraise(type(error), error, _stacktrace)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3packagessix.py", line 769, in reraise
raise value.with_traceback(tb)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connectionpool.py", line 394, in _make_request
conn.request(method, url, **httplib_request_kw)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesurllib3connection.py", line 234, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1253, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1299, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1248, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 1047, in _send_output
self.send(chunk)
File "C:UsersuserAppDataLocalProgramsPythonPython39libhttpclient.py", line 969, in send
self.sock.sendall(data)
File "C:UsersuserAppDataLocalProgramsPythonPython39libssl.py", line 1204, in sendall
v = self.send(byte_view[count:])
File "C:UsersuserAppDataLocalProgramsPythonPython39libssl.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:UsersuserPycharmProjectsFountainAPIAUXDF.py", line 29, in <module>
aux_df_sheet.append_rows(data_list)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesgspreadmodels.py", line 1507, in append_rows
return self.spreadsheet.values_append(range_label, params, body)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesgspreadmodels.py", line 165, in values_append
r = self.client.request('post', url, params=params, json=body)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesgspreadclient.py", line 64, in request
response = getattr(self.session, method)(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesrequestssessions.py", line 590, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesgoogleauthtransportrequests.py", line 482, in request
response = super(AuthorizedSession, self).request(
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesrequestssessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesrequestssessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:UsersuserAppDataLocalProgramsPythonPython39libsite-packagesrequestsadapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', timeout('The write operation timed out'))```

更新:我找到了一个解决方案,只需将df分成两半并将其转移到2块中。它是这样工作的,但我仍然很高兴有一个更干净的解决方案,在一个块中工作。

感谢您的任何输入:)

最新更新