Я хочу подключить мой CSV-файл к SQLite, но принимая пользовательский ввод через d cc .upload python и для каждого файла я хочу отдельную таблицу в одной базе данных - PullRequest
0 голосов
/ 11 января 2020

У меня 13 файлов с разными именами файлов и набором данных. например, имена файлов, такие как A, B, C, D, E .... и т. д. Мой вопрос здесь 1: - Я хочу преобразовать этот CSV-файл в отдельные таблицы в одной базе данных 2: - Если эти все файлы добавлены в отдельную таблицу в одной базе данных. Теперь я хочу добавить файл A еще раз, но набор данных внутри перемешан, а имена столбцов совпадают, я хочу добавить его в существующий файл A в моей базе данных.

import base64
import datetime
import io
from sqlalchemy import create_engine
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_table
import os
from urllib.parse import quote as urlquote
import sqlalchemy as sa
from flask import Flask, send_from_directory
con = sa.create_engine('sqlite:///C:\\Users\\GLOBALTECH\\Downloads\\Python Scripts-20200107T060836Z-001\\Python Scripts\\Greasing_Analytics.db')

#df=pd.read_csv('Daily Greasing Raw Data 2019.csv').to_sql('Grease_Analysis', con, if_exists ='append', index=False)

UPLOAD_DIRECTORY = "C:/Users/Admin/Documents/Python Scripts"

if not os.path.exists(UPLOAD_DIRECTORY):
    os.makedirs(UPLOAD_DIRECTORY)
server = Flask(__name__)
app = dash.Dash(server=server)
@server.route("/download/<path:path>")
def download(path):
    """Serve a file from the upload directory."""
    return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True)
app.layout = html.Div(
    [
        html.H1("File Browser"),
        html.H2("Upload"),
        dcc.Upload(
            id="upload-data",
            children=html.Div(
                ["Drag and drop or click to select a file to upload."]
            ),
            style={
                "width": "100%",
                "height": "60px",
                "lineHeight": "60px",
                "borderWidth": "1px",
                "borderStyle": "dashed",
                "borderRadius": "5px",
                "textAlign": "center",
                "margin": "10px",
            },
            multiple=True,
        ),
  #      html.Button('Convert to SQL', id='button'),
   #     html.Div(id='output-container-button',children='Enter a value and press submit'),
        html.Hr(),  # horizontal line
      #  html.Div(id='output-data-upload'),
        html.H2("File List"),
        html.Ol(id="file-list"),
    ],
    style={"max-width": "500px"},
)
def parse_contents(contents, filename, date):
    content_type, content_string = contents.split(',')

    decoded = base64.b64decode(content_string)
    try:
        if 'csv' in filename:
            # Assume that the user uploaded a CSV file
            df = pd.read_csv(io.StringIO(decoded.decode('utf-8'))).to_sql('Grease_Analysis', con, if_exists ='append', index=False)
            dataa=df
            dataa = pd.read_sql_table("Grease_Analysis", con)
            dataa.drop_duplicates()
            #dataa.to_sql('Grease_Analysis', con, if_exists ='append', index=False)
        elif 'xls' in filename:
            # Assume that the user uploaded an excel file
            df= pd.read_excel(io.BytesIO(decoded)).to_sql('Grease_Analysis', con, if_exists ='append', index=False)
            dataa=df
            dataa = pd.read_sql_table("Grease_Analysis", con)
            dataa.drop_duplicates()
            #dataaa=dataa.to_sql('Grease_Analysis', con, if_exists ='append', index=False)
    except Exception as e:
        print(e)
        return html.Div([
            'There was an error processing this file.'
        ])


    return html.Div([
        html.H5(filename),
        html.H6(datetime.datetime.fromtimestamp(date)),

        dash_table.DataTable(
            data=dataa.to_dict('records'),
            columns=[{"name": i, "id": i, "deletable": True, "selectable": True} for i in dataa.columns],
            editable=True,
            filter_action="native",
            sort_action="native",
            sort_mode="multi",
            column_selectable="single",
            row_selectable="multi",
            row_deletable=True,
            selected_columns=[],
            selected_rows=[],
            page_action="native",
            page_current= 0,
            page_size= 10
        )

        # For debugging, display the raw contents provided by the web browser
        #html.Div('Raw Content'),
       # html.Pre(contents[0:200] + '...', style={
       #     'whiteSpace': 'pre-wrap',
         #   'wordBreak': 'break-all'
      #  })
    ])

def save_file(name, content):
    """Decode and store a file uploaded with Plotly Dash."""
    data = content.encode("utf8").split(b";base64,")[1]
    with open(os.path.join(UPLOAD_DIRECTORY, name), "wb") as fp:
        fp.write(base64.decodebytes(data))
def uploaded_files():
    """List the files in the upload directory."""
    files = []
    for filename in os.listdir(UPLOAD_DIRECTORY):
        path = os.path.join(UPLOAD_DIRECTORY, filename)
        if os.path.isfile(path):
            files.append(filename)
    return files
def file_download_link(filename):
    """Create a Plotly Dash 'A' element that downloads a file from the app."""
    location = "/download/{}".format(urlquote(filename))
    return html.A(filename, href=location)
@app.callback(
    Output("file-list", "children"),
    [Input("upload-data", "filename"), Input("upload-data", "contents")],
)
def update_output(uploaded_filenames, uploaded_file_contents):
    """Save uploaded files and regenerate the file list."""

    if uploaded_filenames is not None and uploaded_file_contents is not None:
        for name, data in zip(uploaded_filenames, uploaded_file_contents):
            save_file(name, data)

    files = uploaded_files()
    if len(files) == 0:
        return [html.Li("No files yet!")]
    else:
        return [html.Li(file_download_link(filename)) for filename in files]
    @app.callback(Output('output-data-upload', 'children'),
              [Input('upload-data', 'contents')],
              [State('upload-data', 'filename'),
               State('upload-data', 'last_modified')])
def update_output(list_of_contents, list_of_names, list_of_dates):
    if list_of_contents is not None:
        children = [
            parse_contents(contents, filename, date) for contents, filename, date in
            zip(list_of_contents, list_of_names, list_of_dates)]
        return children
if __name__ == "__main__":    
    app.run_server()

здесь можно что-нибудь сделать? Пожалуйста, помогите !!!!

...