Skip to content

Instantly share code, notes, and snippets.

@codejake
Created April 24, 2024 15:15
Show Gist options
  • Select an option

  • Save codejake/b4c29217c9256d86bb907467ba6e6a71 to your computer and use it in GitHub Desktop.

Select an option

Save codejake/b4c29217c9256d86bb907467ba6e6a71 to your computer and use it in GitHub Desktop.
Here you go. This Python script reads a CSV file. If you need additional learnin', get in touch.
#!/usr/bin/env python3
# Sample Python script for noobs. This script will run on just about any
# Python 3 install without additional software installation required.
import csv
# Specify the path to your CSV file
file_path = 'data.csv'
# Open the file in read mode
with open(file_path, newline='') as csvfile:
# Create a CSV reader object
reader = csv.reader(csvfile)
# Read the header row separately. Comment the next two lines out if your
# CSV file does not have a header row.
header = next(reader)
print("Header:", header)
# Iterate over each data row in the CSV file
for row in reader:
# Check if the row is not empty
if row:
# Option 1: Print the entire data row
print(row)
# Option 2: Print a specific column of the row. Columns begin with
# a zero.
print(row[0])
# Option 3. Print the last column of the row.
print(row[-1])
else:
print("Empty row detected.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment