Я хотел бы запустить ежедневную работу для разбора фрейма данных на листе Google.
Код начинается с первой пустой строки в листе Google и добавляет данные.
У меня есть два случая. Если я загружаю данные в формате json, это работает, но это данные из 1 строки, которые невозможно прочитать. Я хочу, чтобы формат представлял собой фрейм данных, но если я удаляю оператор BTCJPY = BTCJPY.to_json()
из кода, у меня появляется следующая ошибка:
File "test_cmc.py", line 121, in <module>
worksheet.update_acell("A{}".format(next_row), BTCJPY)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/models.py", line 715, in update_acell
return self.update_cell(*(a1_to_rowcol(label)), value=value)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/models.py", line 739, in update_cell
'values': [[value]]
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/models.py", line 176, in values_update
r = self.client.request('put', url, params=params, json=body)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gspread/client.py", line 73, in request
headers=headers
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 570, in put
return self.request('PUT', url, data=data, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 498, in request
prep = self.prepare_request(req)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/sessions.py", line 441, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/models.py", line 312, in prepare
self.prepare_body(data, files, json)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/requests/models.py", line 462, in prepare_body
body = complexjson.dumps(json)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/simplejson/__init__.py", line 382, in dumps
return _default_encoder.encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/simplejson/encoder.py", line 296, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/simplejson/encoder.py", line 378, in iterencode
return _iterencode(o, 0)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/simplejson/encoder.py", line 273, in default
o.__class__.__name__)
TypeError: Object of type DataFrame is not JSON serializable
Мой код:
def next_available_row(worksheet):
str_list = list(filter(None, worksheet.col_values(1)))
return str(len(str_list)+1)
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
os.chdir(sys.path[0])
creds = ServiceAccountCredentials.from_json_keyfile_name("client_secret1.json", scope)
client = gspread.authorize(creds)
sheet = client.open("Market_data")
worksheet = sheet.worksheet('BTCJPY')
next_row = next_available_row(worksheet)
###GET THE BTC JPY###
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical'
parameters = {
'symbol':'BTC',
'count':'90',
'interval':'daily',
'convert':'JPY'
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
}
session = Session()
session.headers.update(headers)
response = session.get(url, params=parameters)
data = json.loads(response.text)
used_list = [
item['quote']['JPY']
for item in data['data']['quotes']
]
BTCJPY = pd.DataFrame.from_records(used_list)
BTCJPY = BTCJPY.set_index('timestamp')
spread = Spread(creds, 'Market_data')
BTCJPY = BTCJPY.to_json()
worksheet.update_acell("A{}".format(next_row), BTCJPY)