Python Forum
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Statement has no effect"
#14
(Jan-26-2017, 12:37 AM)birdieman Wrote: 1. Why do you create/change a directory called "save" (at lease I think that is what the code does)? It changed the directory that I was using to 'Save'.
Because dictionaries is very useful data structure,some even bully Python of the use of dict anywhere.
(Jan-26-2017, 12:37 AM)birdieman Wrote: 2. After the code reads the file, is there a way to refer to my variables directly, like StartYear, rather than the typical way to refer to an element in a dictionary like data['StartYear']?
There are data['StartYear'] or data.get.('StartYear'),
you can make it a variable StartYear = data['StartYear'] but then you move from your dictionary to Python's build in dictionary Huh
So take a look a this:
>>> date = {}
>>> date['StartYear'] = 2016
>>> date['YourStartAge'] = 25
>>> date['LivExp'] = 80
>>> date
{'LivExp': 80, 'StartYear': 2016, 'YourStartAge': 25}

>>> # Accsess
>>> date['YourStartAge']
25
>>> date.get('YourStartAge')
25
>>> # get() can also be useful to catch stuff that are not in dict.
>>> date.get('hello', 'No in date dict')
'No in date dict'
So to this:
>>> YourStartAge = date['YourStartAge']
>>> YourStartAge
25
>>> # Now you have a varible YourStartAge
>>> # You have really moved it from your dict to Python build in dict(globals())
>>> globals()['YourStartAge']
25
Quote:3. What is the name of the file that the data is stored in?
It's up to you to figure out a name Wink
The dictionary i just made in and out to disk with json.
import json

date = {'LivExp': 25, 'StartYear': 2016, 'YourStartAge': 25}
with open("my_file.json", "w") as j_in:
   json.dump(date, j_in)
with open("my_file.json") as j_out:
   saved_date = json.load(j_out)

print(saved_date) # {'LivExp': 80, 'YourStartAge': 25, 'StartYear': 2016}
So on disk it will be a json file,json.load() take it from disk an it will again be the original dict.
Reply


Messages In This Thread
"Statement has no effect" - by birdieman - Jan-25-2017, 02:36 AM
RE: "Statement has no effect" - by Mekire - Jan-25-2017, 02:52 AM
RE: "Statement has no effect" - by birdieman - Jan-25-2017, 02:59 AM
RE: "Statement has no effect" - by metulburr - Jan-25-2017, 03:04 AM
RE: "Statement has no effect" - by Mekire - Jan-25-2017, 03:17 AM
RE: "Statement has no effect" - by snippsat - Jan-25-2017, 03:55 AM
RE: "Statement has no effect" - by birdieman - Jan-25-2017, 01:35 PM
RE: "Statement has no effect" - by metulburr - Jan-25-2017, 01:40 PM
RE: "Statement has no effect" - by Kebap - Jan-25-2017, 03:56 PM
RE: "Statement has no effect" - by birdieman - Jan-25-2017, 08:38 PM
RE: "Statement has no effect" - by Mekire - Jan-25-2017, 11:40 PM
RE: "Statement has no effect" - by metulburr - Jan-25-2017, 11:47 PM
RE: "Statement has no effect" - by birdieman - Jan-26-2017, 12:57 AM
RE: "Statement has no effect" - by snippsat - Jan-26-2017, 01:37 AM
RE: "Statement has no effect" - by metulburr - Jan-26-2017, 01:38 AM
RE: "Statement has no effect" - by Ofnuts - Jan-26-2017, 02:40 PM
RE: "Statement has no effect" - by metulburr - Jan-26-2017, 03:25 PM
RE: "Statement has no effect" - by birdieman - Jan-27-2017, 12:56 AM
RE: "Statement has no effect" - by metulburr - Jan-27-2017, 01:16 AM
RE: "Statement has no effect" - by birdieman - Jan-27-2017, 01:29 AM
RE: "Statement has no effect" - by metulburr - Jan-27-2017, 01:40 AM
RE: "Statement has no effect" - by Larz60+ - Jan-27-2017, 02:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Copying methods to effect the new owner instead of the old instance Daniel285 2 180 Jun-03-2024, 07:58 AM
Last Post: Gribouillis
  Printing effect sizes for variables in an anova eyavuz21 2 1,066 Feb-01-2023, 02:12 PM
Last Post: eyavuz21
  Help with TypeWriter Effect in Python Rich Extra 0 1,265 May-23-2022, 09:44 PM
Last Post: Extra
  Rotation Effect on live Webcam Feed Leziiy 0 1,664 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  How to fix statement seems to have no effect hernancrespo89 0 6,958 Jan-23-2020, 09:23 PM
Last Post: hernancrespo89
  strange effect from duplicating a file descriptor Skaperen 1 2,118 Feb-18-2019, 08:20 PM
Last Post: Skaperen
  Algorithm effect on the CPU Faruk 3 2,698 Dec-19-2018, 08:57 AM
Last Post: Faruk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020