Skip to content

Instantly share code, notes, and snippets.

@nicolasdanelon
Created August 16, 2023 18:20
Show Gist options
  • Save nicolasdanelon/050c88ef6ca7a24cd8f48a9739774bff to your computer and use it in GitHub Desktop.
Save nicolasdanelon/050c88ef6ca7a24cd8f48a9739774bff to your computer and use it in GitHub Desktop.
create a python dictorionary from a csv
first_name last_name email_address zipcode
John Doe john.doe@example.com 12345
Jane Smith jane.smith@example.com 67890
Alice Johnson alice.johnson@example.com 11223
Bob White bob.white@example.com 44556
import csv
def csv_to_dict(csv_filename):
with open(csv_filename, mode='r') as file:
csv_reader = csv.DictReader(file)
data_list = [row for row in csv_reader]
return data_list
if __name__ == "__main__":
filename = "sample.csv"
result = csv_to_dict(filename)
for entry in result:
print(entry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment