8th day of python challenges 111-117
This commit is contained in:
6
challenges111-117/Books.csv
Normal file
6
challenges111-117/Books.csv
Normal file
@@ -0,0 +1,6 @@
|
||||
To Kill A Mockingbird, Harper Lee, 1960
|
||||
A Brief History of Time, Stephan Hawking, 1988
|
||||
The Man Who Mistook His Wife for a Hat, F.Scott Fitzerald, 1922
|
||||
Pride and Prejudice, Jane Austen, 1813
|
||||
my life through shit, abdullah shallal, 2020
|
||||
just do it, abdullah shallal, 1933
|
|
1
challenges111-117/QuizScore.csv
Normal file
1
challenges111-117/QuizScore.csv
Normal file
@@ -0,0 +1 @@
|
||||
abdullah, 115, 118, 115, 115
|
|
@@ -0,0 +1,5 @@
|
||||
with open('Books.csv', 'w') as file:
|
||||
file.write('To Kill A Mockingbird' + ', ' + 'Harper Lee' + ', ' + '1960\n')
|
||||
file.write('A Brief History of Time' + ', ' + 'Stephan Hawking' + ', ' + '1988\n')
|
||||
file.write('The Man Who Mistook His Wife for a Hat' + ', ' + 'F.Scott Fitzerald' + ', ' + '1922\n')
|
||||
file.write('Pride and Prejudice' + ', ' + 'Jane Austen' + ', ' + '1813\n')
|
||||
|
16
challenges111-117/challenge-112.py
Normal file
16
challenges111-117/challenge-112.py
Normal file
@@ -0,0 +1,16 @@
|
||||
file_read = open('Books.csv', 'r')
|
||||
for row in file_read:
|
||||
print(row)
|
||||
file_read.close()
|
||||
|
||||
ask_name = str(input('Enter Book Name : '))
|
||||
ask_author = str(input('Enter Book Author : '))
|
||||
ask_year = str(input('Enter Book Year Released : '))
|
||||
file_write = open('Books.csv', 'a')
|
||||
file_write.write(ask_name + ', ' + ask_author + ', ' + ask_year + '\n')
|
||||
file_write.close()
|
||||
|
||||
file_read_last = open('Books.csv', 'r')
|
||||
for row in file_read_last:
|
||||
print(row)
|
||||
file_read_last.close()
|
21
challenges111-117/challenge-113.py
Normal file
21
challenges111-117/challenge-113.py
Normal file
@@ -0,0 +1,21 @@
|
||||
file_read = open('Books.csv', 'r')
|
||||
for row in file_read:
|
||||
print(row)
|
||||
file_read.close()
|
||||
|
||||
ask_count_books = int(input('Enter how many books you want to add to file : '))
|
||||
for i in range(ask_count_books):
|
||||
ask_name = str(input('Enter Book Name : '))
|
||||
ask_author = str(input('Enter Book Author : '))
|
||||
ask_year = str(input('Enter Book Year Released : '))
|
||||
file_write = open('Books.csv', 'a')
|
||||
file_write.write(ask_name + ', ' + ask_author + ', ' + ask_year + '\n')
|
||||
file_write.close()
|
||||
|
||||
|
||||
ask_author_to_show = str(input('Enter Book Author to show : '))
|
||||
file_read = open('Books.csv', 'r')
|
||||
for row in file_read:
|
||||
if ask_author_to_show in row:
|
||||
print(row)
|
||||
file_read.close()
|
17
challenges111-117/challenge-114.py
Normal file
17
challenges111-117/challenge-114.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import csv
|
||||
|
||||
file_read = open('Books.csv', 'r')
|
||||
for row in file_read:
|
||||
print(row)
|
||||
file_read.close()
|
||||
|
||||
start_year = int(input('Enter start year : '))
|
||||
end_year = int(input('Enter end year : '))
|
||||
|
||||
tmp = []
|
||||
file_read = open('Books.csv', 'r')
|
||||
file_reader = csv.reader(file_read)
|
||||
for row in file_reader:
|
||||
if start_year < int(row[2]) < end_year:
|
||||
print(row)
|
||||
file_read.close()
|
6
challenges111-117/challenge-115.py
Normal file
6
challenges111-117/challenge-115.py
Normal file
@@ -0,0 +1,6 @@
|
||||
file_read = open('Books.csv', 'r')
|
||||
count_row = 0
|
||||
for row in file_read:
|
||||
print('Row ' + str(count_row) + ' : '+row)
|
||||
count_row += 1
|
||||
file_read.close()
|
40
challenges111-117/challenge-116.py
Normal file
40
challenges111-117/challenge-116.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import csv
|
||||
|
||||
file_read = list(csv.reader(open('Books.csv')))
|
||||
count_row = 0
|
||||
list = []
|
||||
for row in file_read:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
list.append(row)
|
||||
count_row += 1
|
||||
|
||||
ask = int(input('Enter row number to delete : '))
|
||||
list.pop(ask)
|
||||
|
||||
count_row = 0
|
||||
for row in list:
|
||||
print('Row ' + str(count_row) + ' : ' + str(row))
|
||||
count_row += 1
|
||||
|
||||
ask = int(input('Enter row number to edit : '))
|
||||
print('1) Edit book name')
|
||||
print('2) Edit author')
|
||||
print('3) Release Year ')
|
||||
selection_input = int(input('Make a selection 1, 2 or 3 : '))
|
||||
if selection_input == 1:
|
||||
ask_name = str(input('Enter Book Name : '))
|
||||
list[ask][0] = ask_name
|
||||
elif selection_input == 2:
|
||||
ask_author = str(input('Enter Book Author : '))
|
||||
list[ask][1] = ask_author
|
||||
elif selection_input == 3:
|
||||
ask_year = str(input('Enter Book Year Released : '))
|
||||
list[ask][2] = ask_year
|
||||
|
||||
|
||||
i = 0
|
||||
file_write = open('Books.csv', 'w')
|
||||
for row in range(len(list)):
|
||||
file_write.write(list[i][0] + ', ' + list[i][1] + ', ' + list[i][2] + '\n')
|
||||
i += 1
|
||||
file_write.close()
|
23
challenges111-117/challenge-117.py
Normal file
23
challenges111-117/challenge-117.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import csv
|
||||
import random
|
||||
|
||||
score = 0
|
||||
|
||||
ask_name = str(input('Enter your name : '))
|
||||
qustion_1_num_1 = random.randint(1, 100)
|
||||
qustion_1_num_2= random.randint(1, 100)
|
||||
answer_1 = qustion_1_num_1 + qustion_1_num_2
|
||||
ask_answer_1 = int(input(str(qustion_1_num_1) + '+' + str(qustion_1_num_2) + '= '))
|
||||
if answer_1 == ask_answer_1:
|
||||
score += 1
|
||||
|
||||
qustion_2_num_1 = random.randint(1, 100)
|
||||
qustion_2_num_2 = random.randint(1, 100)
|
||||
answer_2 = qustion_2_num_1 + qustion_2_num_2
|
||||
ask_answer_2 = int(input(str(qustion_2_num_1) + '+' + str(qustion_2_num_2) + '= '))
|
||||
if answer_2 == ask_answer_2:
|
||||
score += 1
|
||||
|
||||
file = open('QuizScore.csv', 'a')
|
||||
file.write(ask_name + ', ' + str(ask_answer_1) + ', ' + str(answer_1) + ', ' + str(ask_answer_2) + ', ' + str(answer_2) + '\n')
|
||||
file.close()
|
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