Thursday, October 12, 2023

Read CSV from S3

 import csv

def count_records(csv_file):
    record_count = 0
    first_line = None
    last_line = None

    # Open the CSV file and read it line by line
    with open(csv_file, 'r', newline='') as file:
        reader = csv.reader(file)
        try:
            first_line = next(reader)
            for row in reader:
                last_line = row
                record_count += 1
        except StopIteration:
            pass

    return record_count, first_line, last_line

# Specify the path to your CSV file
csv_file_path = 'path/to/your/file.csv'

# Count the records, get the first and last lines
num_records, first_row, last_row = count_records(csv_file_path)

# Display the results
print(f"Number of records in the CSV file: {num_records}")
print("First line:", first_row)
print("Last line:", last_row)

No comments:

Post a Comment

Read CSV from S3

 import csv def count_records(csv_file):     record_count = 0     first_line = None     last_line = None     # Open the CSV file and read it...