Python Library for Merging CSV Files from Google Cloud Storage
This library provides a function consolidate_csv_from_gcs that simplifies the process of merging multiple CSV files from a Google Cloud Storage (GCS) bucket into a single file.
1. consolidate_csv_from_gcs(bucket_name, prefix, merged_file_name, output_bucket_name, output_bucket_name_prefix):
The consolidate_csv_from_gcs function takes the following arguments:
Parameters:
- bucket_name = "your-bucket-name"
- prefix = "path/to/your/csv/files/" # Include trailing slash
- merged_file_name = "merged_data.csv"
- output_bucket_name = "output-bucket-name"
- output_bucket_name_prefix = "merged/data/" # Optional, defaults to prefix
Return Value:
There is no return value as the result can be seen in the GCS bucket.
Usage:
from Consolidate_csv_files_from_gcs import consolidate_csv_from_gcs
consolidate_csv_from_gcs(
bucket_name='source-bucket',
prefix='data/',
merged_file_name='consolidated.csv',
output_bucket_name='destination-bucket',
output_bucket_name_prefix='processed/'
)
Explanation:
The consolidate_csv_from_gcs function takes the following arguments:
- bucket_name: (str) The name of the GCS bucket containing the CSV files.
- prefix: (str) The prefix to filter files within the bucket (e.g., "path/to/your/csv/files/"). Include a trailing slash.
- merged_file_name: (str) The desired name for the output merged CSV file.
- output_bucket_name: (str) The name of the GCS bucket where the merged file will be saved.
- output_bucket_name_prefix (str, optional): An optional prefix to add to the filename within the output bucket. Defaults to the prefix argument.
Function Behaviour:
- Connects to GCS using storage.Client.
- Retrieves the bucket specified by bucket_name.
- Lists all files with the provided prefix and extension .csv.
- Iterates through each CSV file:
- Reads the content with automatic encoding detection using chardet.
- Creates a Pandas DataFrame from the CSV content.
- Appends the DataFrame to a list.
- Concatenates all DataFrames vertically into a single merged DataFrame.
- Creates a BytesIO object to hold the merged CSV data in memory.
- Writes the merged DataFrame to the BytesIO object using to_csv.
- Connects to the output GCS bucket using storage.Client.
- Creates a new blob object with the desired filename within the output bucket (including any output_bucket_name_prefix).
- Uploads the merged CSV data (from BytesIO) to the output blob as a CSV file (content_type='text/csv').
Additional Notes:
- This library uses the google-cloud-storage and pandas libraries. Ensure they are installed (pip install google-cloud-storage pandas).
- The library automatically detects the encoding of the CSV files using chardet.
- The merged file is written to a memory buffer (BytesIO) before uploading to GCS for efficiency.
I hope this README clarifies the usage of these functions for managing Alteryx workflows through Python. Feel free to ask if you have any further questions.
Metadata
Release files for Consolidate-csv-files-from-gcs 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Consolidate_csv_files_from_gcs-0.1.tar.gz | 3.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Consolidate_csv_files_from_gcs-0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.1 kB
Release files / Consolidate_csv_files_from_gcs-0.1.tar.gz
| Download URL | Consolidate_csv_files_from_gcs-0.1.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
59eeb3a98142b6b9c87e74636eb7e5bf9fe3c191d33bd3bc0bc2cdb05b9b4711
|
|
BLAKE2b-256 checksum How to use checksums |
1b74afd0166421023cf995bee44b78011f3c8476b245da6b09175d98d61c8b66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / Consolidate_csv_files_from_gcs-0.1-py3-none-any.whl
| Download URL | Consolidate_csv_files_from_gcs-0.1-py3-none-any.whl |
|---|---|
| Size | 4.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26645073af5ca04af56d4f469e5d065cfaef71dbca2f43064927093999adce81
|
|
BLAKE2b-256 checksum How to use checksums |
f4443d7e21ae4abfb833cebeac891c1d911879b30768ad2348b1096ff50377f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|