8th day of python challenges 111-117
This commit is contained in:
3
challenges111-117/realpython-csv/employee_addresses.txt
Normal file
3
challenges111-117/realpython-csv/employee_addresses.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
name,address,date joined
|
||||
john smith,1132 Anywhere Lane Hoboken NJ, 07030,Jan 4
|
||||
erica meyers,1234 Smith Lane Hoboken NJ, 07030,March 2
|
3
challenges111-117/realpython-csv/employee_birthday.txt
Normal file
3
challenges111-117/realpython-csv/employee_birthday.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
name,department,birthday month
|
||||
John Smith,Accounting,November
|
||||
Erica Meyers,IT,March
|
2
challenges111-117/realpython-csv/employee_file.csv
Normal file
2
challenges111-117/realpython-csv/employee_file.csv
Normal file
@@ -0,0 +1,2 @@
|
||||
John Smith,Accounting,November
|
||||
Erica Meyers,IT,March
|
|
3
challenges111-117/realpython-csv/employee_file2.csv
Normal file
3
challenges111-117/realpython-csv/employee_file2.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
emp_name,dept,birth_month
|
||||
John Smith,Accounting,November
|
||||
Erica Meyers,IT,March
|
|
6
challenges111-117/realpython-csv/hrdata-noheader.csv
Normal file
6
challenges111-117/realpython-csv/hrdata-noheader.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
Graham Chapman,03/15/14,50000.00,10
|
||||
John Cleese,06/01/15,65000.00,8
|
||||
Eric Idle,05/12/14,45000.00,10
|
||||
Terry Jones,11/01/13,70000.00,3
|
||||
Terry Gilliam,08/12/14,48000.00,7
|
||||
Michael Palin,05/23/13,66000.00,8
|
|
7
challenges111-117/realpython-csv/hrdata.csv
Normal file
7
challenges111-117/realpython-csv/hrdata.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
Name,Hire Date,Salary,Sick Days remaining
|
||||
Graham Chapman,03/15/14,50000.00,10
|
||||
John Cleese,06/01/15,65000.00,8
|
||||
Eric Idle,05/12/14,45000.00,10
|
||||
Terry Jones,11/01/13,70000.00,3
|
||||
Terry Gilliam,08/12/14,48000.00,7
|
||||
Michael Palin,05/23/13,66000.00,8
|
|
7
challenges111-117/realpython-csv/hrdata_modified.csv
Normal file
7
challenges111-117/realpython-csv/hrdata_modified.csv
Normal file
@@ -0,0 +1,7 @@
|
||||
Employee,Hired,Salary,Sick Days
|
||||
Graham Chapman,2014-03-15,50000.0,10
|
||||
John Cleese,2015-06-01,65000.0,8
|
||||
Eric Idle,2014-05-12,45000.0,10
|
||||
Terry Jones,2013-11-01,70000.0,3
|
||||
Terry Gilliam,2014-08-12,48000.0,7
|
||||
Michael Palin,2013-05-23,66000.0,8
|
|
59
challenges111-117/realpython-csv/train-csv-lib.py
Normal file
59
challenges111-117/realpython-csv/train-csv-lib.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import csv
|
||||
|
||||
|
||||
# --------- READING CSV ----------
|
||||
|
||||
# 1 - Built-in CSV Library
|
||||
# here return an array --> ['John Smith', 'Accounting', 'November']
|
||||
|
||||
with open('employee_birthday.txt') as csv_file:
|
||||
csv_reader = csv.reader(csv_file)
|
||||
count = 0
|
||||
for row in csv_reader:
|
||||
print(row)
|
||||
# if count == 0:
|
||||
# print(f'Columns are {", ".join(row)}')
|
||||
# count += 1
|
||||
# else:
|
||||
# print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
|
||||
# count += 1
|
||||
print(f'Processed {count} lines.')
|
||||
|
||||
|
||||
# 2 - Reading CSV Files Into a Dictionary
|
||||
# return dictionary of arrays --> [('name', 'John Smith'), ('department', 'Accounting'), ('birthday month', 'November')]
|
||||
|
||||
with open('employee_birthday.txt') as csv_file:
|
||||
csv_reader = csv.DictReader(csv_file)
|
||||
count = 0
|
||||
for row in csv_reader:
|
||||
print(row)
|
||||
# if count == 0:
|
||||
# print(f'Column names are {", ".join(row)}')
|
||||
# count += 1
|
||||
# print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
|
||||
# count += 1
|
||||
print(f'Processed {count} lines.')
|
||||
|
||||
|
||||
|
||||
# --------- WRITING CSV ----------
|
||||
|
||||
# 1 - Built-in CSV Library
|
||||
|
||||
with open('employee_file.csv', mode='w') as employee_file:
|
||||
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
|
||||
|
||||
employee_writer.writerow(['John Smith', 'Accounting', 'November'])
|
||||
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
|
||||
|
||||
|
||||
# 2 - Reading CSV Files Into a Dictionary
|
||||
|
||||
with open('employee_file2.csv', mode='w') as csv_file:
|
||||
fieldnames = ['emp_name', 'dept', 'birth_month']
|
||||
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
||||
|
||||
writer.writeheader()
|
||||
writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting', 'birth_month': 'November'})
|
||||
writer.writerow({'emp_name': 'Erica Meyers', 'dept': 'IT', 'birth_month': 'March'})
|
32
challenges111-117/realpython-csv/train-pandas-lib.py
Normal file
32
challenges111-117/realpython-csv/train-pandas-lib.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import pandas
|
||||
|
||||
# --------- READING CSV ----------
|
||||
|
||||
df = pandas.read_csv('hrdata.csv')
|
||||
print(df)
|
||||
|
||||
|
||||
df = pandas.read_csv('hrdata.csv', index_col='Name')
|
||||
print(df)
|
||||
|
||||
|
||||
df = pandas.read_csv('hrdata.csv', index_col='Name', parse_dates=['Hire Date'])
|
||||
print(df)
|
||||
|
||||
|
||||
dfNh = pandas.read_csv('hrdata-noheader.csv',
|
||||
index_col='Employee',
|
||||
parse_dates=['Hired'],
|
||||
header=0,
|
||||
names=['Employee', 'Hired', 'Salary', 'Sick Days'])
|
||||
print(dfNh)
|
||||
|
||||
|
||||
# --------- WRITING CSV ----------
|
||||
|
||||
df = pandas.read_csv('hrdata.csv',
|
||||
index_col='Employee',
|
||||
parse_dates=['Hired'],
|
||||
header=0,
|
||||
names=['Employee', 'Hired', 'Salary', 'Sick Days'])
|
||||
df.to_csv('hrdata_modified.csv')
|
Reference in New Issue
Block a user