forked from UtrechtUniversity/SWORDS-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(readme_delimiter): add script to process and delimit README cont…
…ent (in single cell) in CSV files
- Loading branch information
1 parent
3dd7fa2
commit 00fb339
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
collect_variables/scripts/parse_readme/column_readme_delimiter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
This script processes the 'readme' column in a CSV file by adding specific delimiters | ||
around its content to ensure that the entire content fits within a single cell, even if | ||
it contains special characters or other delimiters. The processed CSV file is then saved | ||
to a specified output location. | ||
""" | ||
|
||
import argparse | ||
import pandas as pd | ||
|
||
|
||
def process_readme_column(input_csv, output_csv, delimiter=','): | ||
""" | ||
Process the 'readme' column in the CSV file to change its delimiter. | ||
Args: | ||
input_csv (str): Path to the input CSV file. | ||
output_csv (str): Path to save the output CSV file. | ||
delimiter (str): The delimiter to use for the readme content. | ||
""" | ||
try: | ||
# Read the CSV file | ||
data_frame = pd.read_csv(input_csv, sep=delimiter, engine='python') | ||
|
||
# Process the 'readme' column to replace delimiters inside it | ||
if 'readme' in data_frame.columns: | ||
data_frame['readme'] = data_frame['readme'].apply( | ||
lambda x: f"README_start {str(x)} README_end" | ||
) | ||
else: | ||
print("The 'readme' column does not exist in the input CSV file.") | ||
return | ||
|
||
# Save the updated CSV file | ||
data_frame.to_csv(output_csv, sep=delimiter, index=False) | ||
print(f"Processed CSV saved to {output_csv}") | ||
|
||
except FileNotFoundError: | ||
print(f"File {input_csv} not found.") | ||
except pd.errors.ParserError: | ||
print(f"Parsing error occurred while reading {input_csv}.") | ||
except Exception as error: # Catching a more specific error | ||
print(f"An error occurred: {error}") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description="Process the 'readme' column in a CSV file to change its delimiter." | ||
) | ||
parser.add_argument('--input', required=True, help="Path to the input CSV file.") | ||
parser.add_argument('--output', required=True, help="Path to save the output CSV file.") | ||
parser.add_argument( | ||
'--delimiter', default=',', help="Delimiter to use in the CSV file (default is ',')." | ||
) | ||
|
||
arguments = parser.parse_args() | ||
|
||
process_readme_column(arguments.input, arguments.output, arguments.delimiter) |