This project is a Python script designed to split a CSV file containing data into three parts: JSON, a database, and CSV. The script provides flexibility to customize the distribution percentages for each destination. The primary goal of this script is to help users efficiently split large datasets into various formats for different purposes.
- Randomly shuffles the data for even distribution among the output files.
- Supports custom distribution percentages for JSON, database, and CSV.
- Saves the data into a JSON file, a CSV file, and a specified database table.
- Displays statistics after data insertion, such as the number of rows in each output file and the percentage of data retained.
- Python 3.x
- pandas
- numpy
- sqlalchemy
-
Ensure you have Python 3.x installed.
-
Install the required libraries by running the following command: pip install pandas numpy sqlalchemy
-
Prepare your CSV file and place it in the same directory as the Python script.
-
Update the script variables according to your configuration:
csv_file_path
: The filename of the CSV file to be split.json_file_path
: The filename for the JSON output file.db_server
: The server name or address of your SQL Server.db_name
: The name of the database where the table will be created.db_driver
: The ODBC driver for the SQL Server.db_table
: The name of the table in the database.trusted_connection
: Set to "yes" if using trusted connection; otherwise, set to "no".distribution
: A tuple representing the distribution percentages for JSON, database, and CSV, respectively.header
: Set toTrue
if the CSV file has a header; otherwise, set toFalse
.
- Run the Python script: python script_name.py
Suppose you have a large CSV file named cleaned_repositoriesV2.csv
that contains data on repositories. You can use this script to split the data into JSON, CSV (without header), and insert a portion into a database table.
- Make sure the CSV file has a header in the first row.
- Before running the script, ensure that your SQL Server is running and accessible from your Python environment.