gpt4 book ai didi

python - 如何在不保存在磁盘上的情况下将 pandas 数据帧上传为 csv 流?

转载 作者:太空宇宙 更新时间:2023-11-04 08:37:21 26 4
gpt4 key购买 nike

我想将 pandas 数据帧作为 csv 文件上传到服务器,而不将其保存在磁盘上。有没有办法创建一个或多或少的“假 csv”文件来伪装成真实文件?

下面是一些示例代码:

首先,我从 sql 查询中获取数据并将其存储为数据框。在 upload_ga_data 函数中,我想使用以下逻辑:

 media = MediaFileUpload('df',
mimetype='application/octet-stream',
resumable=False)

完整示例:

from __future__ import print_function
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.errors import HttpError
from apiclient.http import MediaFileUpload
import pymysql
import pandas as pd
con = x

ga_query = """
SELECT XXXXX
"""

df = pd.read_sql_query(ga_query,con)

df.to_csv('ga_export.csv', sep=',', encoding='utf-8', index = False)

def upload_ga_data():
try:
media = MediaFileUpload('ga_export.csv',
mimetype='application/octet-stream',
resumable=False)
daily_upload = service.management().uploads().uploadData(
accountId=accountId,
webPropertyId=webPropertyId,
customDataSourceId=customDataSourceId,
media_body=media).execute()
print ("Upload was successfull")
except TypeError as error:
# Handle errors in constructing a query.
print ('There was an error in constructing your query : %s' % error)

最佳答案

所需的行为可以使用 stream 实现:

to create a more or less "fake csv" file which pretends to be a real file

Python 使文件描述符(使用open)和Stream(使用io.StringIO)的行为相似。然后在任何可以使用文件描述符的地方也可以使用字符串流。

The easiest way to create a text stream is with open(), optionally specifying an encoding:

f = open("myfile.txt", "r", encoding="utf-8")

In-memory text streams are also available as StringIO objects:

f = io.StringIO("some initial text data")

The text stream API is described in detail in the documentation of TextIOBase.

在 Pandas 中,您可以使用任何 function在其 signature 中包含 path_or_buf 参数,例如to_csv:

DataFrame.to_csv(path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression=None, quoting=None, quotechar='"', line_terminator='\n', chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, decimal='.')

以下代码将 CSV 格式的虚拟 DataFrame 导出到字符串流(不是物理文件,内存中的八位字节流):

import io
import pandas as pd

df = pd.DataFrame(list(range(10)))

stream = io.StringIO()
df.to_csv(stream, sep=";")

当您想访问流内容时,只需发出:

>>> stream.getvalue()
';0\n0;0\n1;1\n2;2\n3;3\n4;4\n5;5\n6;6\n7;7\n8;8\n9;9\n'

它无需使用真实文件即可返回内容。

关于python - 如何在不保存在磁盘上的情况下将 pandas 数据帧上传为 csv 流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com