Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Introduction to Programming, 27.11.2023
- print("Welcome!")
- # CREATE A NEW TEXTFILE, weekdays.txt, contents:
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
- # NEW FILE
- # open a file: weekdays.txt, r-mode => read
- file_handle = open("weekdays.txt", "r")
- # read the contents of a file into a variable
- contents = file_handle.read()
- file_handle.close()
- # print the contents
- print(contents)
- # NEW FILE
- # open a file: weekdays.txt, r-mode => read
- file_handle = open("weekdays.txt", "r")
- counter = 1
- # read line by line
- while True:
- # we might get some weird numbering
- # issues because of all the newlines
- line = file_handle.readline()
- print(f"{counter}. {line}")
- counter = counter + 1
- # if we reach the end of the file
- # => break out of loop
- if not line:
- break
- file_handle.close()
- # NEW FILE
- # open a file: weekdays.txt, r-mode => read
- file_handle = open("weekdays.txt", "r")
- # read data as usual and use split()
- # to make a list of the data
- content = file_handle.read()
- file_handle.close()
- # this is now a plain Python list,
- # we can use any method we learnt
- # earlier in the course
- lines = content.split("\n")
- # get amount of lines in list
- amount = len(lines)
- # loop through weekdays
- # with a row number
- for index in range(amount):
- line = lines[index]
- print(f"{index + 1}. {line}")
- # NEW FILE
- # open a file: mynotes.txt, w-mode => write
- # write completely replaces the file content
- # if something is saved into the file
- file_handle = open("mynotes.txt", "w", encoding="utf-8")
- # ask text from user
- message = input("Write your message:\n")
- # write data into the file
- file_handle.write(message)
- file_handle.close()
- # NEW FILE
- # open a file: mynotes.txt, a-mode => append
- # append places the new data into the end
- # of the previous data
- file_handle = open("mynotes.txt", "a", encoding="utf-8")
- # ask text from user
- message = input("Write your message:\n")
- # write new data into the file
- # let's add a new line to add the data
- # into separate lines in the file
- file_handle.write(message + "\n")
- file_handle.close()
- # NEW TEXT FILE: app_data.json, contents:
- {
- "name": "Rovaniemi",
- "population": 62933,
- "county": "Lapland"
- }
- # NEW FILE
- import var_dump as vd
- import json
- # open a file: app_data.json, r-mode => read
- file_handle = open("app_data.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON (which is text) to Python data
- city = json.loads(content)
- print(city['name'])
- print(city['population'])
- # vd.var_dump(content)
- # print()
- # vd.var_dump(city)
- # NEW FILE
- import json
- # test data in Python format
- phone = {
- "name": "Nokia 3310",
- "release_year": 2000,
- "battery": "1200mAh",
- "camera": False,
- "weight": 133
- }
- # convert Python data => JSON format (which is text)
- content = json.dumps(phone)
- # open a file and save the JSON into it
- file_handle = open("myphone.json", "w")
- file_handle.write(content)
- file_handle.close()
- print("Thank you for adding your phone!")
- # NEW TEXT FILE: cities.json , contents:
- [
- {
- "name": "Finland",
- "population": 5536146,
- "capital": "Helsinki"
- },
- {
- "name": "Sweden",
- "population": 10402070,
- "capital": "Stockholm"
- },
- {
- "name": "France",
- "population": 67413000,
- "capital": "Paris"
- }
- ]
- # NEW FILE
- import json
- # open a file: cities.json, r-mode => read
- file_handle = open("cities.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON (which is text) to Python data
- # this file is a list of dictionaries
- cities = json.loads(content)
- # cities is a list, for-loop =>
- for city in cities:
- print(city['name'])
- print(city['population'])
- print()
- # NEW TEXT FILE : americancities.json, contents:
- [
- {
- "name": "Los Angeles",
- "population": 3898747,
- "state": "California"
- },
- {
- "name": "Miami",
- "population": 6166488,
- "state": "Florida"
- },
- {
- "name": "Denver",
- "population": 715522,
- "state": "Colorado"
- }
- ]
- # NEW FILE
- import json
- # PART 1: Read original data from a file
- # open a file: app_data.json, r-mode => read
- file_handle = open("americancities.json", "r")
- content = file_handle.read()
- file_handle.close()
- # convert JSON (which is text) to Python data
- cities = json.loads(content)
- # loop through the cities and print out
- # details of each city
- for city in cities:
- print(city['name'])
- print(city['state'])
- print(city['population'])
- print()
- # PART 2 : Ask the user for the new city
- # and build the data
- city_name = input("New city, name:\n")
- city_population = input("New city, population:\n")
- city_population = int(city_population)
- city_state = input("New city, state:\n")
- # combine all variables to a new dictionary
- new_city = {
- "name": city_name,
- "population": city_population,
- "state": city_state
- }
- # add the new city as a new city
- # to the original list of cities
- cities.append(new_city)
- # PART 3: Save the new version of the data
- # and replace the old version
- raw_data = json.dumps(cities)
- # save the new version to file
- file_handle = open("americancities.json", "w")
- file_handle.write(raw_data)
- file_handle.close()
- print("New city saved! Thank you!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement