There are 5 little program below which work with plain text files. You can download these little programs, or you can copy and paste these into the IDLE editor.
You can download all these here: src/
Write a program which creates a file containing 10000 numbers, then write a program which reads that file then prints out the sum of the numbers.
Modify the program to create 10000 numbers between 1 and 100, then find the sum of these.
Download:p9a.py # p9a.py : create text file of random numbers import random f = open('numbers.txt', 'w') for i in range(10000): n = random.randint(1,10) f.write( str(n) + "\n") f.close()
Download:p9b.py # p9b.py : add up the integers in a text file g = open('numbers.txt',errors='ignore') sum = 0 while True: n = g.readline() if n == '': break sum = sum + int(n) print("The sum is", sum) g.close()
Download the file: pop_county_state.csv
Find the population of Carlton County, MN by year.
Q: Find the population of Hennepin County, MN in 2019.
Download:p9c.py # p9c.py : find Carlton County population by year, and 2020 g = open('pop_county_state.csv',errors='ignore') while True: line = g.readline() if line == '': break if "Carlton County, MN" in line: line = line.replace('"','') field = line.split(',') print(field,':', field) if field == "2020": print("In year 2020, Carlton County has ",field," residents.\n") g.close()
Download the file: city_lat_lon.txt
Find the lat/lon of Duluth,MN
Find the lat/lon of Minneapolis,MN
Download:p9d.py # p9d.py : find City lat/lon g = open('city_lat_lon.txt',errors='ignore') while True: line = g.readline() if line == '': break if "Duluth,MN" in line: code,lat,lon,name = line.split() print("The 3 letter code for ", name," is ", code,"\n") print("lat/lon=",lat,"/",lon,"\n") g.close()
Download the file: oz.txt
Count the occurences of the word "the" in the book.
Count how many times Dorothy and Toto occur in the book
Download:p9e.py # p9e.py : count a word in a text word = "the" g = open('oz.txt',errors='ignore') s = g.read() s = s.lower() print(word," occurs this many times:", s.count(word)) g.close()