Week 8: Text Processing (Reading and Writing Files)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Week 8

Text processing
(Reading and writing files)
Week 8
Today’s Tasks
Tasks for Today!
▪ Tasks
▪ Merge a set of files into a new file
▪ Handle coordinate information from a file
▪ Handle temperature information from a file
Task 1 | Merge files
▪ Merge a set of input files into a single output file
○ All contents in the input files are put into the output file

Alan Mathison Turing was a British


pioneering computer scientist, Alan Mathison Turing was a British
mathematician, logician, cryptanalyst, pioneering computer scientist,
philosopher, mathematical biologist, and mathematician, logician, cryptanalyst,
marathon and ultra distance runner. philosopher, mathematical biologist, and
marathon and ultra distance runner.
Turing is widely considered to be the father
of theoretical computer science and
Turing is widely considered to be the father
artificial intelligence.
of theoretical computer science and
artificial intelligence.
Task 1 | Merge files
▪ Define a function ‘merge’ with two parameters
○ 1st parameter : names of input files ( list of string )
○ 2nd parameters : a name of a string output file ( string )
○ No output in this function

▪ Merge input files by calling the function ‘merge’


○ merge ( [“kaist1.txt”, “kaist2.txt”, “kaist3.txt”], “result.txt” )
○ merge ( [“kaist1.txt”, “kaist2.txt”], “result.txt” )

▪ Use elice_utils.send_file() function


○ Then, you can see a link to download the file
Task 2 | Handle coordinate information
1. Read and print data from the file ‘average-latitude-longitude-
countries.csv'
○ Each line (except for the head) contains country code, country name,
latitude and longitude, which are comma-separated
■ E.g. “KR”, “Korea, Republic of”, 37, 127.5
■ -90.0° ≤ latitude ≤ 90.0°, -180.0° ≤ longitude ≤ 180.0°
■ Caution: several country names contain comma.

○ Make two lists and print them


■ List of tuples → ( country code, country name )
■ List of tuples → ( country code, (latitude, longitude) )
■ Data type
- country code, country name → string
- latitude, longitude → float
Task 2 | Handle coordinate information
2. Print the names of all country whose location lies in the south
of the equator.

3. Let the user enter a country code, and then print the full name
of the corresponding country.

Tip. You can reuse the lists of tuples in task 2-1.


Then, you don’t need to read the file again.
Task 3 | Handle temperature information
▪ The file (“tpmon.txt”) includes England’s monlty temperatures
information for the years 1723 ~ 1970

1. Read the data and print average winter and summer


temperatures for each year
○ Winter average = ( January’s + February’s ) / 2
○ Summer average = ( July’s + August’s ) / 2
○ Print every year and winter/summer averages
in a nicely formatted table
Task 3 | Handle temperature information
2. Write the contents in another format
○ To the file ‘tpmon.csv’
○ Each year and its 12 monthly temperatures in one line
○ Write in CSV format (i.e. comma-separate the data)
○ Try to open the file in Excel
questions?

You might also like