8th day of python challenges 111-117

This commit is contained in:
abd.shallal
2019-08-04 15:26:35 +03:00
parent b04c1b055f
commit 627802c383
3215 changed files with 760227 additions and 491 deletions

View 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

View File

@@ -0,0 +1,3 @@
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March

View File

@@ -0,0 +1,2 @@
John Smith,Accounting,November
Erica Meyers,IT,March
1 John Smith Accounting November
2 Erica Meyers IT March

View File

@@ -0,0 +1,3 @@
emp_name,dept,birth_month
John Smith,Accounting,November
Erica Meyers,IT,March
1 emp_name dept birth_month
2 John Smith Accounting November
3 Erica Meyers IT March

View 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
1 Graham Chapman 03/15/14 50000.00 10
2 John Cleese 06/01/15 65000.00 8
3 Eric Idle 05/12/14 45000.00 10
4 Terry Jones 11/01/13 70000.00 3
5 Terry Gilliam 08/12/14 48000.00 7
6 Michael Palin 05/23/13 66000.00 8

View 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
1 Name Hire Date Salary Sick Days remaining
2 Graham Chapman 03/15/14 50000.00 10
3 John Cleese 06/01/15 65000.00 8
4 Eric Idle 05/12/14 45000.00 10
5 Terry Jones 11/01/13 70000.00 3
6 Terry Gilliam 08/12/14 48000.00 7
7 Michael Palin 05/23/13 66000.00 8

View 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
1 Employee Hired Salary Sick Days
2 Graham Chapman 2014-03-15 50000.0 10
3 John Cleese 2015-06-01 65000.0 8
4 Eric Idle 2014-05-12 45000.0 10
5 Terry Jones 2013-11-01 70000.0 3
6 Terry Gilliam 2014-08-12 48000.0 7
7 Michael Palin 2013-05-23 66000.0 8

View 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'})

View 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')