diff --git a/Basics/Hindi/12_read_write_file/Exercise/exercise_1_poem.py b/Basics/Exercise/13_read_write_files/exercise_1_poem.py similarity index 100% rename from Basics/Hindi/12_read_write_file/Exercise/exercise_1_poem.py rename to Basics/Exercise/13_read_write_files/exercise_1_poem.py diff --git a/Basics/Exercise/13_read_write_files/exercise_2_stocks.py b/Basics/Exercise/13_read_write_files/exercise_2_stocks.py new file mode 100644 index 00000000..f71442ba --- /dev/null +++ b/Basics/Exercise/13_read_write_files/exercise_2_stocks.py @@ -0,0 +1,12 @@ +with open("stocks.csv", "r") as f, open("output.csv", "w") as out: + out.write("Company Name,PE Ratio, PB Ratio\n") + next(f) # This will skip first line in the file which is a header + for line in f: + tokens = line.split(",") + stock = tokens[0] + price = float(tokens[1]) + eps = float(tokens[2]) + book = float(tokens[3]) + pe = round(price / eps, 2) + pb = round(price / book, 2) + out.write(f"{stock},{pe},{pb}\n") diff --git a/Basics/Hindi/12_read_write_file/Exercise/output.csv b/Basics/Exercise/13_read_write_files/output.csv similarity index 100% rename from Basics/Hindi/12_read_write_file/Exercise/output.csv rename to Basics/Exercise/13_read_write_files/output.csv diff --git a/Basics/Hindi/12_read_write_file/Exercise/poem.txt b/Basics/Exercise/13_read_write_files/poem.txt similarity index 100% rename from Basics/Hindi/12_read_write_file/Exercise/poem.txt rename to Basics/Exercise/13_read_write_files/poem.txt diff --git a/Basics/Hindi/12_read_write_file/read_write_file_exercise.md b/Basics/Exercise/13_read_write_files/read_write_file_exercise.md similarity index 51% rename from Basics/Hindi/12_read_write_file/read_write_file_exercise.md rename to Basics/Exercise/13_read_write_files/read_write_file_exercise.md index 86e5ded5..d73d08d7 100644 --- a/Basics/Hindi/12_read_write_file/read_write_file_exercise.md +++ b/Basics/Exercise/13_read_write_files/read_write_file_exercise.md @@ -1,10 +1,10 @@ ## Exercise: Python Read Write File -1. [poem.txt](https://github.com/codebasics/py/blob/master/Basics/Hindi/12_read_write_file/Exercise/poem.txt) contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in your python program and find out words with maximum occurance. +1. [poem.txt](https://github.com/codebasics/py/blob/master/Basics/Exercise/13_read_write_files/poem.txt) contains famous poem "Road not taken" by poet Robert Frost. You have to read this file in your python program and find out words with maximum occurance. -[Solution](https://github.com/codebasics/py/blob/master/Basics/Hindi/12_read_write_file/Exercise/exercise_1_poem.py) +[Solution](https://github.com/codebasics/py/blob/master/Basics/Exercise/13_read_write_files/exercise_2_stocks.py) -2. [stocks.csv](https://github.com/codebasics/py/blob/master/Basics/Hindi/12_read_write_file/Exercise/stocks.csv) contains stock price, earnings per share and book value. You are writing a stock market application that will process this file and create a new file +2. [stocks.csv](https://github.com/codebasics/py/blob/master/Basics/Exercise/13_read_write_files/stocks.csv) contains stock price, earnings per share and book value. You are writing a stock market application that will process this file and create a new file with financial metrics such as pe ratio and price to book ratio. These are calculated as, ``` pe ratio = price / earnings per share @@ -26,4 +26,4 @@ Output.csv should look like this, |Reliance|22.23|2.25| |Tata Steel|4.39|0.68| -[Solution](https://github.com/codebasics/py/blob/master/Basics/Hindi/12_read_write_file/Exercise/exercise_2_stocks.py) +[Solution](https://github.com/codebasics/py/blob/master/Basics/Exercise/13_read_write_files/exercise_2_stocks.py) diff --git a/Basics/Hindi/12_read_write_file/Exercise/stocks.csv b/Basics/Exercise/13_read_write_files/stocks.csv similarity index 100% rename from Basics/Hindi/12_read_write_file/Exercise/stocks.csv rename to Basics/Exercise/13_read_write_files/stocks.csv diff --git a/Basics/Hindi/12_read_write_file/Exercise/exercise_2_stocks.py b/Basics/Hindi/12_read_write_file/Exercise/exercise_2_stocks.py deleted file mode 100644 index beff07a7..00000000 --- a/Basics/Hindi/12_read_write_file/Exercise/exercise_2_stocks.py +++ /dev/null @@ -1,12 +0,0 @@ -with open("stocks.csv","r") as f, open("output.csv","w") as out: - out.write("Company Name,PE Ratio, PB Ratio\n") - next(f) # This will skip first line in the file which is a header - for line in f: - tokens=line.split(',') - stock = tokens[0] - price = float(tokens[1]) - eps = float(tokens[2]) - book = float(tokens[3]) - pe = round(price/eps,2) - pb = round(price/book,2) - out.write(f"{stock},{pe},{pb}\n")