1# pip install pandas
2import pandas as pd
3
4# Read the csv file
5data = pd.read_csv('data.csv')
6
7# Print it out if you want
8print(data)
1import csv
2
3with open('names.csv', 'w') as csvfile:
4 fieldnames = ['first_name', 'last_name']
5 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
6
7 writer.writeheader()
8 writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
9 writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
10 writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
11
1>>> import csv
2>>> with open('names.csv', newline='') as csvfile:
3... reader = csv.DictReader(csvfile)
4... for row in reader:
5... print(row['first_name'], row['last_name'])
6...
7Eric Idle
8John Cleese
9
10>>> print(row)
11{'first_name': 'John', 'last_name': 'Cleese'}
12
1with open(r'c:\dl\FrameRecentSessions.csv') as csv_file:
2 csv_reader = csv.reader(csv_file, delimiter=',')
3 line_count = 0
4 for row in csv_reader:
5 if line_count == 0:
6 print(f'Column names are {", ".join(row)}')
7 line_count += 1
8 else:
9 print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
10 line_count += 1
11 print(f'Processed {line_count} lines.')
1import csv
2
3with open('employee_birthday.txt') as csv_file:
4 csv_reader = csv.reader(csv_file, delimiter=',')
5 line_count = 0
6 for row in csv_reader:
7 if line_count == 0:
8 print(f'Column names are {", ".join(row)}')
9 line_count += 1
10 else:
11 print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
12 line_count += 1
13 print(f'Processed {line_count} lines.')
14
1import csv
2
3with open('employee_birthday.txt', mode='r') as csv_file:
4 csv_reader = csv.DictReader(csv_file)
5 line_count = 0
6 for row in csv_reader:
7 if line_count == 0:
8 print(f'Column names are {", ".join(row)}')
9 line_count += 1
10 print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
11 line_count += 1
12 print(f'Processed {line_count} lines.')
13