小编Shi*_*dra的帖子

无法解析使用Flask上传的.csv文件

我正在尝试上传CSV文件,对其进行处理以生成结果,然后回写(下载)包含结果的新CSV文件.我是Flask的新手,我无法获得一个"正确"的csv.reader对象来迭代和处理.这是到目前为止的代码,

__author__ = 'shivendra'
from flask import Flask, make_response, request
import csv

app = Flask(__name__)

def transform(text_file_contents):
    return text_file_contents.replace("=", ",")


@app.route('/')
def form():
    return """
        <html>
            <body>
                <h1>Transform a file demo</h1>

                <form action="/transform" method="post" enctype="multipart/form-data">
                    <input type="file" name="data_file" />
                    <input type="submit" />
                </form>
            </body>
        </html>
    """

@app.route('/transform', methods=["POST"])
def transform_view():
    file = request.files['data_file']
    if not file:
        return "No file"

    file_contents = file.stream.read().decode("utf-8")
    csv_input = csv.reader(file_contents)
    print(file_contents)
    print(type(file_contents))
    print(csv_input)
    for row in csv_input:
        print(row)

    result = transform(file_contents)

    response = …
Run Code Online (Sandbox Code Playgroud)

python csv flask

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

Python-从Excel文件读取时间时未获得正确的日期时间

I have an excel file which has 3 columns as either date-time or date or time fields. I am reading it via xlrd package and I am getting time as milliseconds I suppose and when I am trying to convert it back to datetime I am getting wrong results.

I tried with converting the file to csv as well. That too doesn't help and I get weird datetime format which I am not able to comprehend.

Here is what I …

python csv excel datetime xlrd

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

使用除特定列之外的StandardScaler()来规范化pandas数据帧

所以我有一个数据框,我通过合并训练(标记)和测试(未标记)数据帧形成.并且为了取消附加测试数据框,我保留了一个列,如果该行属于训练或测试,则该列具有标识符.现在我必须规范化所有列中的所有值,除了这一列"Sl No." 但我没有找到任何方法通过这一栏.这就是我在做的事情

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler

data_norm = data_x_filled.copy() #Has training + test data frames combined to form single data frame
normalizer = StandardScaler()
data_array = normalizer.fit_transform(data_norm)
data_norm = pd.DataFrame(data_array,columns = data_norm.columns).set_index(data_norm.index)
Run Code Online (Sandbox Code Playgroud)

我只想排除"Sl No."栏目 规范化但希望在规范化后保留它.

python normalization pandas scikit-learn

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

将python(pandas)数据框写入SQL数据库错误

我试图将一个python数据框放到MS SQL DB,我收到以下错误

功能

def put_to_server(df):       # df is a pandas data frame
   server="KORNBSVM04\MSSQLSERVER2012"
   Driver="{SQL Server}"
   userid=''
   pswd=''
   cnxn = pyodbc.connect(driver=Driver, server=server, database="CIHOTLINE",uid=userid, pwd=pswd)
   cur=cnxn.cursor()
   df.to_sql(name='dbo.test',con=cnxn)
Run Code Online (Sandbox Code Playgroud)

错误

 File "C:\Python27\lib\site-packages\pandas\core\generic.py", line 950, in to_sql
index_label=index_label)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 475, in to_sql
index_label=index_label)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1084, in to_sql
index_label=index_label)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 543, in __init__
if self.pd_sql.has_table(self.name):
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1094, in has_table
return len(self.execute(query).fetchall()) > 0
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1041, in execute
raise_with_traceback(ex)
File "C:\Python27\lib\site-packages\pandas\io\sql.py", line 1030, in execute
cur.execute(*args) …
Run Code Online (Sandbox Code Playgroud)

python sql sql-server pyodbc pandas

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

ggplot到png - 自动拉伸图像

我正在生成一个ggplot plot并将其保存为.png图像.虽然在Rstudio中生成的绘图根据y轴的值拉伸,但是当我将其保存为时,我得到一个方形图像.png.

如何在.png表格中自动获得最佳拉伸图像?

# Function to store ggplot plot object as .png image file
savePlot <- function(myPlot, filename) {
  png(filename)
  print(myPlot)
  dev.off()
}

# ggplot object
normalized_bar_plot = ggplot(dat, aes(factor(temp), norm, fill = type)) + 
  geom_bar(stat="identity", position = "dodge") + ylab("Normalized count")+xlab(features[i])+
  scale_fill_brewer(palette = "Set1")

filename = paste0("image_", features[i], ".png")
savePlot(normalized_bar_plot, filename)
Run Code Online (Sandbox Code Playgroud)

png r image ggplot2

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

标签 统计

python ×4

csv ×2

pandas ×2

datetime ×1

excel ×1

flask ×1

ggplot2 ×1

image ×1

normalization ×1

png ×1

pyodbc ×1

r ×1

scikit-learn ×1

sql ×1

sql-server ×1

xlrd ×1