Student will be able to
.read()
local files in memory .read()
to read a specific number of characters .readlines()
to read data from file as a list of lines .readlines()
to read data from file as a list of lines .readline()
to read data from file a line at a time .strip()
to remove new line characters .write()
to write data to a new local file .seek()
to set file read or write location Task 1
Download and open the file.
rainbow.txt
, using curl
https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow # [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
.readlines()
# [ ] Open rainbow.txt in read mode & read as list with .readlines()
#Answer:
rainbow_file = open('rainbow.txt', 'r')
rainbow_colors = rainbow_file.readlines()
# [ ] sort rainbow_colors list, iterate the list to print each color
#Answer:
rainbow_colors.sort()
for color in rainbow_colors:
print(color[:-1])
Task 2
Create a program that reads from a file to display city name and average temperature in Celsius.
!curl
to download https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv as mean_temp.txt
# [ ] The Weather: import world_mean_team.csv as mean_temp.txt
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
'r'
mode. headings
and print()
. headings
to a list using .split(',')
which splits on each comma, print()
the list.# [ ] The Weather: open file, read/print first line, convert line to list (splitting on comma)
#Answer:
world_mean_team_file = open('mean_temp.txt', 'r')
heading = world_mean_team_file.readline()
heading_list = list(heading[:-1].split(','))
#print (world_mean_team_line)
print (heading_list)
city_temp
variable. .split(',')
for each .readline()
in the loop. >Tips & Hints:
headings
to determine the city_temp indexes to use. city_temp
to lists with .split(',')
. # [ ] The Weather: use while loop to print city and highest monthly average temp in celsius
#Answer:
print(heading_list[0],heading_list[2])
while True:
city_temp = world_mean_team_file.readline()
if (len(city_temp)==0):
break
else:
city_temp_list = list(city_temp[:-1].split(','))
print(city_temp_list[0],city_temp_list[2])
# [ ] use curl to download https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi as pi.txt
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o pi.txt
name
for variable called seed
. .seek()
with the value of seed
to set the initial pointer location reading the file. digit
and assign it the value of reading one character from the file. guess
variable value from users input
- "enter a single digit guess or "q" to quit". correct
and wrong
counter variables to 0
(zero). # [ ] Set up the project files and initial values
#Answer:
pi_file = open("pi.txt",'r')
pi_file.seek(0,0)
pi_text = pi_file.read()
user_name = input('Type your name: ')
print('Hi ',user_name)
seed = len(user_name)
digit = pi_file.seek(seed)
guess = input('enter a single digit guess or "q" to quit')
correct = 0
wrong = 0
print('digit = ',digit,'gues = ',guess)
guess
is a digit string¶then in the loop:
digit
( read from pi file) is "." read the next character for digit digit
is "\n" increment seed
and use seed
to set the pointer uing .seek()
guess
is equal to digit
guess
equals digit
: print "correct" and increment the varible named correct
guess
not equal digit
: print "incorrect" and increment the variable named wrong
end the while loop when user enters any non-digit(s) for guess
, like "q".
correct
and wrong
values within a message to the user. #Answer:
if (digit == "."):
digit = pi_file.seek(seed+1)
elif (digit == "\n"):
seed+=1
digit = pi_file.seek(seed)
if (int(digit) == int(guess)):
print("correct")
correct += 1
else:
print("wrong")
wrong += 1
print('digit = ',digit,'gues = ',guess)
print('correct = ',correct," ; incorrect = ",wrong)
Terms of use Privacy & cookies © 2017 Microsoft