Python is perfect language for this task because it has great libraries for sqlite and CSV DataFrames. Show Creating a sqlite databasesqlite is a lightweight database that can be started as an empty text file. You can create the file with import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()1 or with this equivalent Python code: from pathlib import Path Path('my_data.db').touch() A zero byte text file is a great starting point for a lightweight database! Creating sqlite tableCreate a database connection and cursor to execute queries. import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor() Execute a query that’ll create a import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()2 table with import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()3 and import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()4 columns. c.execute('''CREATE TABLE users (user_id int, username text)''') Load CSV file into sqlite tableSuppose you have the following import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()5 file: user_id,username 1,pokerkid 2,crazyken Pandas makes it easy to load this CSV data into a sqlite table: import pandas as pd # load the data into a Pandas DataFrame users = pd.read_csv('users.csv') # write the data to a sqlite table users.to_sql('users', conn, if_exists='append', index = False) The import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()6 method makes it easy to write DataFrames to databases. Fetch values from sqlite tableFetch all the rows from the import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()2 table: c.execute('''SELECT * FROM users''').fetchall() # [(1, 'pokerkid'), (2, 'crazyken')] The import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()8 method returns an array of tuples. import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()9 returns a c.execute('''CREATE TABLE users (user_id int, username text)''')0 object. Cursors can be thought of as iterators in the database world. Load another CSV into the databasesSuppose you have the following c.execute('''CREATE TABLE users (user_id int, username text)''')1 file: order_id,user_id,item_name 1,1,speaker 2,1,phone 3,2,spoon 4,2,fork 5,2,speaker Create a table and then load the orders data into the database. c.execute('''CREATE TABLE orders (order_id int, user_id int, item_name text)''') orders = pd.read_csv('orders.csv') # load to DataFrame orders.to_sql('orders', conn, if_exists='append', index = False) # write to sqlite table Fetch results of database joinJoin the import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()2 and c.execute('''CREATE TABLE users (user_id int, username text)''')3 tables on the import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()3 value and print the results: c.execute('''SELECT * FROM users u LEFT JOIN orders o ON u.user_id = o.user_id''') c.fetchall() Here’s the array that’s returned: [(1, 'pokerkid', 1, 1, 'speaker'), (1, 'pokerkid', 2, 1, 'phone'), (2, 'crazyken', 3, 2, 'spoon'), (2, 'crazyken', 4, 2, 'fork'), (2, 'crazyken', 5, 2, 'speaker')] You can also read the SQL query directly into a Pandas DataFrame. import sqlite3 conn = sqlite3.connect('my_data.db') c = conn.cursor()0 Next stepsPython’s build in sqlite library coupled with Pandas DataFrames makes it easy to load CSV data into sqlite databases. sqlite databases are great for local experimentation and are used extensively on mobile phones. It’s a great database when you’d like relational database query functionality without the overhead of Postgres. I was helping a friend through how to solve problems by dissecting them into small tiny problems and solve them one-by-one, So here is the code assignment as the main problem we are trying to solve. The Problem: Steps
Stack we will use:
Requirements
0 Get Flask readyNow let's test if Flask works by starting to create our main file 5
We can run the file by 6 then heading to 7.If everything goes as planned, you will get the rendered message. Next step is to create the HTML page which will be responsible for uploading the CSV files. 1- Create file upload formLet's create 8 directory and our HTML upload form in it.
2- Upload the files to the serverNow we need to accept the incoming files from this form, let's add the reasonable function in 5
As everything is set, we can test the file upload now at 0 If everything is ok, you can see the uploaded files at 13- Parse CSV fileTo parse the CSV file data into rows, we will use 2 the data analysis and manipulation library. |