Here's a Python script that reads and prints a CSV file with semicolon separators and quoted fields:
#!/usr/bin/env python3
import csv
import argparse
def print_csv(csv_filename):
"""Read and print a CSV file with semicolon separators and quoted fields."""
try:
with open(csv_filename, 'r', encoding='utf-8') as file:
# Configure CSV reader for semicolon delimiter and quoted fields
csv_reader = csv.reader(file, delimiter=';', quotechar='"')
# Print each row
for row_num, row in enumerate(csv_reader, 1):
print(f"Row {row_num}: {row}")
# Alternatively, print each field on a separate line
# for field_num, field in enumerate(row, 1):
# print(f" Field {field_num}: {field}")
except FileNotFoundError:
print(f"Error: File '{csv_filename}' not found.")
except Exception as e:
print(f"Error reading file: {e}")
def main():
# Set up command line argument parser
parser = argparse.ArgumentParser(description='Print a CSV file with semicolon separators and quoted fields')
parser.add_argument('--csv_filename', type=str, required=True,
help='Path to the CSV file to print')
# Parse arguments
args = parser.parse_args()
# Print the CSV file
print(f"Reading CSV file: {args.csv_filename}\n")
print_csv(args.csv_filename)
if __name__ == "__main__":
main()
To use this script:
- Save it to a file (e.g.,
print_csv.py)
- Make it executable:
chmod +x print_csv.py
- Run it:
python print_csv.py --csv_filename yourfile.csv
The script will handle CSV files where fields are separated by semicolons and each field is quoted. For example, if your CSV file looks like this:
"Name";"Age";"City"
"John Doe";"30";"New York"
"Jane Smith";"25";"Los Angeles"
The output will show each row with its fields as a list. If you prefer a different output format, you can uncomment the alternative printing section in the code to display each field on a separate line.