小编use*_*542的帖子

读取文件数据而不将其保存在Flask中

我正在写我的第一个烧瓶应用程序.我正在处理文件上传,基本上我想要的是读取上传文件的数据/内容而不保存它,然后将其打印在结果页面上.是的,我假设用户总是上传文本文件.

这是我正在使用的简单上传功能:

@app.route('/upload/', methods=['GET', 'POST'])
def upload():
   if request.method == 'POST':
      file = request.files['file']
      if file:
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        a = 'file uploaded'

return render_template('upload.html', data = a)
Run Code Online (Sandbox Code Playgroud)

现在,我正在保存文件,但我需要的是'a'变量来包含文件的内容/数据..任何想法?

python flask

89
推荐指数
3
解决办法
8万
查看次数

更新gensim word2vec模型

我在gensim中有一个word2vec模型,训练超过98892个文档.对于句子数组中不存在的任何给定句子(即我训练模型的集合),我需要用该句子更新模型,以便下次查询它会给出一些结果.我是这样做的:

new_sentence = ['moscow', 'weather', 'cold']
model.train(new_sentence)
Run Code Online (Sandbox Code Playgroud)

并将其打印为日志:

2014-03-01 16:46:58,061 : INFO : training model with 1 workers on 98892 vocabulary and 100 features
2014-03-01 16:46:58,211 : INFO : reached the end of input; waiting to finish 1 outstanding jobs
2014-03-01 16:46:58,235 : INFO : training on 10 words took 0.1s, 174 words/s
Run Code Online (Sandbox Code Playgroud)

现在,当我使用类似的new_sentence查询大多数肯定(as model.most_similar(positive=new_sentence))时,它会发出错误:

Traceback (most recent call last):
 File "<pyshell#220>", line 1, in <module>
 model.most_similar(positive=['moscow', 'weather', 'cold'])
 File "/Library/Python/2.7/site-packages/gensim/models/word2vec.py", line 405, in most_similar
 raise KeyError("word '%s' not …
Run Code Online (Sandbox Code Playgroud)

gensim word2vec

29
推荐指数
3
解决办法
2万
查看次数

排序日期字符串列表

我有一个任意的日期字符串列表(mm-yyyy)如下:

d = ['09-2012', '04-2007', '11-2012', '05-2013', '12-2006', '05-2006', '08-2007'...]
Run Code Online (Sandbox Code Playgroud)

我需要先将这个列表按年级(升序)排序,然后按月(升序)等级排序.这样逻辑排序可以是:

d_ordered = ['05-2006', '12-2006', '04-2007', '08-2007', '09-2012', '11-2012', '05-2013' ...]
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

python datetime

25
推荐指数
2
解决办法
4万
查看次数

将d3.js气泡转换为基于强制/重力的布局

我有一组数据,我使用d3.js可视化.我以气泡的形式表示数据点,其中气泡的配置如下:

var dot = svg.selectAll("g")
            .data(data)
            .enter()
            .append("g");

dot.append("circle")
  .attr("class", "dot")
  .attr("cx", function(d) { return xp(x(d)); })
  .attr("cy", function(d) { return yp(y(d)); })
  .style("fill", function(d) { return colorp(color(d)); })
  .attr("r", function(d) { return radiusp(radius(d)*2000000); });

dot.append("text")
   .attr("x", function(d) { return xp(x(d)); })
   .attr("y", function(d) { return yp(y(d)); })
   .text(function(d) { return d.name; })
Run Code Online (Sandbox Code Playgroud)

其中xp,yp,colorp和radiusp定义如下:

var xp = d3.scale.log().domain([300, 1e5]).range([0, width]),
   yp = d3.scale.linear().domain([10, 85]).range([height, 0]),
   radiusp = d3.scale.sqrt().domain([0, 5e8]).range([0, 40]),
   colorp = d3.scale.category10();
Run Code Online (Sandbox Code Playgroud)

此时,气泡在其位置上显示为静态(其中位置由xp和yp定义),而气泡的大小基本上来自radiusp,颜色由colorp定义.

现在我正如这个例子那样展示它们: http ://bl.ocks.org/mbostock/4063269

在此输入图像描述

我需要的是以这种形式显示它们: http …

javascript svg d3.js force-layout circle-pack

9
推荐指数
1
解决办法
4448
查看次数

在python列表中保留日期顺序时插入缺少的日期

我有一个包含[yyyy,value]项目的列表列表,每个子列表按递增年份排序.这是一个示例:

A = [
    [[2008, 5], [2009, 5], [2010, 2], [2011, 5], [2013, 17]], 
    [[2008, 6], [2009, 3], [2011, 1], [2013, 6]], [[2013, 9]], 
    [[2008, 4], [2011, 1], [2013, 4]], 
    [[2010, 3], [2011, 3], [2013, 1]], 
    [[2008, 2], [2011, 4], [2013, 1]], 
    [[2009, 1], [2010, 1], [2011, 3], [2013, 3]], 
    [[2010, 1], [2011, 1], [2013, 5]], 
    [[2011, 1], [2013, 4]], 
    [[2009, 1], [2013, 4]], 
    [[2008, 1], [2013, 3]], 
    [[2009, 1], [2013, 2]], 
    [[2013, 2]], 
    [[2011, 1], [2013, 1]],
    [[2013, 1]], …
Run Code Online (Sandbox Code Playgroud)

python datetime numpy

5
推荐指数
1
解决办法
1145
查看次数

从磁盘加载的Python字典在内存中占用太多空间

我有一个腌制在磁盘上的字典,大小约为780 Megs(在磁盘上).但是,当我将该字典加载到内存中时,其大小意外膨胀到大约6千兆字节.无论如何都要保持内存中实际文件大小的大小,(我的意思是如果它在内存中需要大约1演出,但是6演出是一种奇怪的行为).pickle模块有问题,还是应该以其他格式保存字典?

这是我加载文件的方式:

import pickle

with open('py_dict.pickle', 'rb') as file:
    py_dict = pickle.load(file)
Run Code Online (Sandbox Code Playgroud)

任何想法,帮助,将不胜感激.

python file-io pickle

4
推荐指数
1
解决办法
557
查看次数

从python的dicts列表中选择最高值

在一系列dicts列表中:

 A = [
       [{'x': 1, 'y': 0}, {'x': 2, 'y': 3}, {'x': 3, 'y': 4}, {'x': 4, 'y': 7}],

       [{'x': 1, 'y': 0}, {'x': 2, 'y': 2}, {'x': 3, 'y': 13}, {'x': 4, 'y': 0}],

       [{'x': 1, 'y': 20}, {'x': 2, 'y': 4}, {'x': 3, 'y': 0}, {'x': 4, 'y': 8}]

     ]
Run Code Online (Sandbox Code Playgroud)

我需要从每个dicts列表中检索最高的'y'值...所以结果列表将包含:

 Z = [(4, 7), (3,13), (1,20)]
Run Code Online (Sandbox Code Playgroud)

在A中,'x'是每个字典的关键,而'y'是每个字典的值.

有任何想法吗?谢谢.

python numpy list

3
推荐指数
2
解决办法
279
查看次数

将Python字符串转换为字典

我有一个字符串:

A = "{user_id:34dd833,category:secondary,items:camera,type:sg_ser}"
Run Code Online (Sandbox Code Playgroud)

我需要将它转换为python字典,以便:

A = {"user_id":"34dd833", "category": "secondary", "items": "camera", "type": "sg_ser"}
Run Code Online (Sandbox Code Playgroud)

最重要的是,还有两个问题:

1:"items"键应该有多个值,例如:

A = {"user_id":34dd833, "category": "secondary", "items": "camera,vcr,dvd", "type": "sg_ser"}
Run Code Online (Sandbox Code Playgroud)

这显然是以字符串的形式出现的:

A = "{user_id:34dd833,category:secondary,items:camera,vcr,dvd,type:sg_ser}"
Run Code Online (Sandbox Code Playgroud)

因此,基于逗号分离推广任何内容都变得毫无用处.

2:字符串的顺序也可以是随机的.所以,字符串也可以这样:

A = "{category:secondary,type:sg_ser,user_id:34dd833,items:camera,vcr,dvd}"
Run Code Online (Sandbox Code Playgroud)

这使得任何按顺序假设为虚假的过程.

在这种情况下该怎么办?非常感谢.

python json dictionary

0
推荐指数
1
解决办法
134
查看次数