tuomasvaltanen

Untitled

Dec 3rd, 2021 (edited)
633
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # files and JSON in Python
  2.  
  3. # create file weekdays.txt:
  4.  
  5. Monday
  6. Tuesday
  7. Wednesday
  8. Thursday
  9. Friday
  10. Saturday
  11. Sunday
  12.  
  13. # reading a file
  14.  
  15. file_handle = open("weekdays.txt", "r")
  16. content = file_handle.read()
  17.  
  18. print(content)
  19.  
  20. # NEW FILE
  21.  
  22. file_handle = open("weekdays.txt", "r")
  23.  
  24. counter = 1
  25.  
  26. # process the file
  27. while True:
  28.     # next line
  29.     line = file_handle.readline()
  30.  
  31.     # end of file
  32.     if not line:
  33.         break
  34.  
  35.     # print the line
  36.     print(f"{counter} : {line}")
  37.     counter = counter + 1
  38.  
  39.  
  40. # NEW FILE
  41.  
  42. # open the file
  43. file_handle = open("weekdays.txt", "r")
  44.  
  45. # load the content of the file into a variable
  46. content = file_handle.read()
  47.  
  48. # split content into a list of texts
  49. lines = content.split("\n")
  50.  
  51. # how many lines
  52. amount = len(lines)
  53.  
  54. # loop through lines, numbered list
  55. for index in range(amount):
  56.     line = lines[index]
  57.     print(f"{index + 1}. {line}")
  58.  
  59. # NEW FILE
  60.  
  61. # open file, force to use utf-8 in order to see special characters etc. correctly
  62. # also important if using different languages that involve other characters
  63. # than in standard English
  64. file_handle = open("testing.txt", "w", encoding="utf-8")
  65.  
  66. text = input("Write your message:\n")
  67.  
  68. # file_handle.write("Is this still working?")
  69. file_handle.write(text)
  70.  
  71. print("File saved.")
  72.  
  73. # create a city.json -file:
  74.  
  75. {
  76.    "name":"Rovaniemi",
  77.    "population":62933,
  78.    "county":"Lapland"
  79. }
  80.  
  81. # NEW FILE
  82.  
  83. import json
  84.  
  85. # open the file, read content, close connection
  86. file_handle = open("city.json", "r")
  87. content = file_handle.read()
  88. file_handle.close()
  89.  
  90. # convert JSON text => Python dictionary (or list, depends on data)
  91. data = json.loads(content)
  92.  
  93. # data is now a dictionary, use as you wish
  94. print(data['name'])
  95. print(data['population'])
  96.  
  97. # NEW FILE
  98.  
  99. import json
  100.  
  101. phone = {
  102.     "name": "Nokia 3310",
  103.     "release_year": 2000,
  104.     "battery": "1000mAh",
  105.     "camera": False,
  106.     "weight": 133
  107. }
  108.  
  109. file_handle = open("phone.json", "w")
  110.  
  111. #print(phone)
  112. data = json.dumps(phone)
  113. file_handle.write(data)
  114. file_handle.close()
  115.  
  116. print("The phone was saved.")
  117.  
  118. # first create cities.json -file:
  119.  
  120. [
  121.   {
  122.     "name": "Los Angeles",
  123.     "population": 3898747,
  124.     "state": "California"
  125.   },
  126.   {
  127.     "name": "Miami",
  128.     "population": 6166488,
  129.     "state": "Florida"
  130.   },
  131.   {
  132.     "name": "Denver",
  133.     "population": 715522,
  134.     "state": "Colorado"
  135.   }
  136. ]
  137.  
  138. # NEW FILE
  139.  
  140. import json
  141.  
  142. # open the file, read content, close connection
  143. file_handle = open("cities.json", "r")
  144. content = file_handle.read()
  145. file_handle.close()
  146.  
  147. data = json.loads(content)
  148.  
  149. for city in data:
  150.     print(city['name'])
RAW Paste Data