【问题标题】:Retrieve a csv uploaded to GridFS (mongodb)检索上传到 GridFS (mongodb) 的 csv
【发布时间】:2021-06-30 07:44:06
【问题描述】:

我使用 python 将 csv 上传到 gridfs,现在我想在我的新 python 代码中使用这个 csv。我试过这个:

client = MongoClient("localhost", 27017)
db = client["grid_file"]
collection = db["fs.files"]
name="csv"
fs = gridfs.GridFS(db)

gout = fs.get_last_version(name)

fout = open('s.csv', 'wb')
fout.write(gout.read())

它对我有用,但是这种方法将 csv 保存在我的计算机中,我希望将 csv 保存在我的代码中的数据框之类的变量中,但不将其保存在我的计算机中。 有人可以帮助我吗?

【问题讨论】:

    标签: python pandas mongodb gridfs


    【解决方案1】:

    我终于明白了,我把我的代码留在这里,以防将来有人可以帮助你

    client = MongoClient("localhost", 27017)
    db = client["grid_file"]
    collection = db["fs.files"]
    name="csv"
                    
    fs = gridfs.GridFS(db)
    
    gout = fs.get_last_version(name)
            
    fout = []
    fout.append(gout.read())
            
    fout2 = str(fout[0])
    fout3= fout2.split("\\r\\n")
    ListaUlt = []
    for i in range(len(fout3)-1):
        ListaUlt.append(fout3[i+1].split(','))
            
    df = pd.DataFrame(ListaUlt, columns =['userID', 'activity', 'timestamp', 'x-axis', 'y-axis', 'z-axis'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2015-07-18
      • 2016-01-01
      • 2015-08-22
      • 2012-03-22
      • 2021-07-03
      • 2021-11-24
      相关资源
      最近更新 更多