Advertisement
tuomasvaltanen

Untitled

Nov 27th, 2023 (edited)
907
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.32 KB | None | 0 0
  1. # Introduction to Programming, 27.11.2023
  2. print("Welcome!")
  3.  
  4. # CREATE A NEW TEXTFILE, weekdays.txt, contents:
  5. Monday
  6. Tuesday
  7. Wednesday
  8. Thursday
  9. Friday
  10. Saturday
  11. Sunday
  12.  
  13. # NEW FILE
  14.  
  15. # open a file: weekdays.txt, r-mode => read
  16. file_handle = open("weekdays.txt", "r")
  17.  
  18. # read the contents of a file into a variable
  19. contents = file_handle.read()
  20. file_handle.close()
  21.  
  22. # print the contents
  23. print(contents)
  24.  
  25. # NEW FILE
  26.  
  27. # open a file: weekdays.txt, r-mode => read
  28. file_handle = open("weekdays.txt", "r")
  29.  
  30. counter = 1
  31.  
  32. # read line by line
  33. while True:
  34.     # we might get some weird numbering
  35.     # issues because of all the newlines
  36.     line = file_handle.readline()
  37.     print(f"{counter}. {line}")
  38.  
  39.     counter = counter + 1
  40.  
  41.     # if we reach the end of the file
  42.     # => break out of loop
  43.     if not line:
  44.         break
  45.  
  46. file_handle.close()
  47.  
  48. # NEW FILE
  49.  
  50. # open a file: weekdays.txt, r-mode => read
  51. file_handle = open("weekdays.txt", "r")
  52.  
  53. # read data as usual and use split()
  54. # to make a list of the data
  55. content = file_handle.read()
  56. file_handle.close()
  57.  
  58. # this is now a plain Python list,
  59. # we can use any method we learnt
  60. # earlier in the course
  61. lines = content.split("\n")
  62.  
  63. # get amount of lines in list
  64. amount = len(lines)
  65.  
  66. # loop through weekdays
  67. # with a row number
  68. for index in range(amount):
  69.     line = lines[index]
  70.     print(f"{index + 1}. {line}")
  71.  
  72. # NEW FILE
  73.  
  74. # open a file: mynotes.txt, w-mode => write
  75. # write completely replaces the file content
  76. # if something is saved into the file
  77. file_handle = open("mynotes.txt", "w", encoding="utf-8")
  78.  
  79. # ask text from user
  80. message = input("Write your message:\n")
  81.  
  82. # write data into the file
  83. file_handle.write(message)
  84. file_handle.close()
  85.  
  86. # NEW FILE
  87.  
  88. # open a file: mynotes.txt, a-mode => append
  89. # append places the new data into the end
  90. # of the previous data
  91. file_handle = open("mynotes.txt", "a", encoding="utf-8")
  92.  
  93. # ask text from user
  94. message = input("Write your message:\n")
  95.  
  96. # write new data into the file
  97. # let's add a new line to add the data
  98. # into separate lines in the file
  99. file_handle.write(message + "\n")
  100. file_handle.close()
  101.  
  102. # NEW TEXT FILE: app_data.json, contents:
  103.  
  104. {
  105.     "name": "Rovaniemi",
  106.     "population": 62933,
  107.     "county": "Lapland"
  108. }
  109.  
  110. # NEW FILE
  111.  
  112. import var_dump as vd
  113. import json
  114.  
  115. # open a file: app_data.json, r-mode => read
  116. file_handle = open("app_data.json", "r")
  117. content = file_handle.read()
  118. file_handle.close()
  119.  
  120. # convert JSON (which is text) to Python data
  121. city = json.loads(content)
  122.  
  123. print(city['name'])
  124. print(city['population'])
  125.  
  126. # vd.var_dump(content)
  127. # print()
  128. # vd.var_dump(city)
  129.  
  130. # NEW FILE
  131.  
  132. import json
  133.  
  134. # test data in Python format
  135. phone = {
  136.     "name": "Nokia 3310",
  137.     "release_year": 2000,
  138.     "battery": "1200mAh",
  139.     "camera": False,
  140.     "weight": 133
  141. }
  142.  
  143. # convert Python data => JSON format (which is text)
  144. content = json.dumps(phone)
  145.  
  146. # open a file and save the JSON into it
  147. file_handle = open("myphone.json", "w")
  148. file_handle.write(content)
  149. file_handle.close()
  150.  
  151. print("Thank you for adding your phone!")
  152.  
  153. # NEW TEXT FILE: cities.json , contents:
  154.  
  155. [
  156.    {
  157.    "name": "Finland",
  158.    "population": 5536146,
  159.    "capital": "Helsinki"
  160.    },
  161.    {
  162.    "name": "Sweden",
  163.    "population": 10402070,
  164.    "capital": "Stockholm"
  165.    },
  166.    {
  167.    "name": "France",
  168.    "population": 67413000,
  169.    "capital": "Paris"
  170.    }
  171. ]
  172.  
  173. # NEW FILE
  174.  
  175. import json
  176.  
  177. # open a file: cities.json, r-mode => read
  178. file_handle = open("cities.json", "r")
  179. content = file_handle.read()
  180. file_handle.close()
  181.  
  182. # convert JSON (which is text) to Python data
  183. # this file is a list of dictionaries
  184. cities = json.loads(content)
  185.  
  186. # cities is a list, for-loop =>
  187. for city in cities:
  188.     print(city['name'])
  189.     print(city['population'])
  190.     print()
  191.  
  192. # NEW TEXT FILE : americancities.json, contents:
  193.  
  194. [
  195.    {
  196.    "name": "Los Angeles",
  197.    "population": 3898747,
  198.    "state": "California"
  199.    },
  200.    {
  201.    "name": "Miami",
  202.    "population": 6166488,
  203.    "state": "Florida"
  204.    },
  205.    {
  206.    "name": "Denver",
  207.    "population": 715522,
  208.    "state": "Colorado"
  209.    }
  210. ]
  211.  
  212. # NEW FILE
  213.  
  214. import json
  215.  
  216.  
  217. # PART 1: Read original data from a file
  218. # open a file: app_data.json, r-mode => read
  219. file_handle = open("americancities.json", "r")
  220. content = file_handle.read()
  221. file_handle.close()
  222.  
  223. # convert JSON (which is text) to Python data
  224. cities = json.loads(content)
  225.  
  226. # loop through the cities and print out
  227. # details of each city
  228. for city in cities:
  229.     print(city['name'])
  230.     print(city['state'])
  231.     print(city['population'])
  232.     print()
  233.  
  234.  
  235. # PART 2 : Ask the user for the new city
  236. # and build the data
  237.  
  238. city_name = input("New city, name:\n")
  239. city_population = input("New city, population:\n")
  240. city_population = int(city_population)
  241. city_state = input("New city, state:\n")
  242.  
  243. # combine all variables to a new dictionary
  244. new_city = {
  245.     "name": city_name,
  246.     "population": city_population,
  247.     "state": city_state
  248. }
  249.  
  250. # add the new city as a new city
  251. # to the original list of cities
  252. cities.append(new_city)
  253.  
  254. # PART 3: Save the new version of the data
  255. # and replace the old version
  256. raw_data = json.dumps(cities)
  257.  
  258. # save the new version to file
  259. file_handle = open("americancities.json", "w")
  260. file_handle.write(raw_data)
  261. file_handle.close()
  262.  
  263. print("New city saved! Thank you!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement