[Tutor] Fwd: Re: Using files to read data

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jun 27 07:01:49 EDT 2017


Forwarding to list

Please always use ReplyAll or ReplyList when responding to list mail.



-------- Forwarded Message --------

i apologize.  i guess it didn’t attach correctly.
my issue is how do i get it out of my file and use it. 

*the json file, its only about a fifth of it but it should serve its
purpose*
[
0.9888888888888889,
0.011111111111111112,
"Mon Jun 26 20:37:34 2017"
]
[
0.9777777777777777,
0.022222222222222223,
"Mon Jun 26 20:37:34 2017"
]
[
0.9666666666666667,
0.03333333333333333,
"Mon Jun 26 20:37:34 2017"
]
[
0.9555555555555556,
0.044444444444444446,
"Mon Jun 26 20:37:34 2017"
]
[
0.9444444444444444,
0.05555555555555555,
"Mon Jun 26 20:37:34 2017"
]
[
0.9333333333333333,
0.06666666666666667,
"Mon Jun 26 20:37:34 2017"
]
[
0.9222222222222223,
0.07777777777777778,
"Mon Jun 26 20:37:34 2017"
]
[
0.9111111111111111,
0.08888888888888889,
"Mon Jun 26 20:37:34 2017"
]
[
0.9,
0.1,
"Mon Jun 26 20:37:34 2017"
]
[
0.8888888888888888,
0.1111111111111111,
"Mon Jun 26 20:37:34 2017"
]
[
0.8777777777777778,
0.12222222222222222,
"Mon Jun 26 20:37:34 2017"
]
[
0.8666666666666667,
0.13333333333333333,
"Mon Jun 26 20:37:34 2017"
]
[
0.8555555555555555,
0.14444444444444443,
"Mon Jun 26 20:37:34 2017"
]
[
0.8444444444444444,
0.15555555555555556,
"Mon Jun 26 20:37:34 2017”

*the code that creates it*

import json
import time

def xyz(heading, forward_time):
    """get x and y increments of 360 degrees"""
    
    if heading in range(1, 91):
        north = heading
        east = 90 - heading
        y = (north / 90) * forward_time
        x = (east / 90) * forward_time
        now_time = time.ctime()
        
        xyz = [x, y, now_time]
        
        xyz_save = "xyz_save.json"


        with open(xyz_save, "a") as stamp:
            json.dump(xyz, stamp, indent=0)
            stamp.write("\n")

        return xyz
        
    elif heading in range(91, 181):
        east = heading - 90
        south = 180 - heading
        y = (south / 90) * forward_time
        x = (east / -90) * forward_time
        now_time = time.ctime()
        
        xyz = [x, y, now_time]
        
        xyz_save = "xyz_save.json"


        with open(xyz_save, "a") as stamp:
            json.dump(xyz, stamp, indent=0)
            stamp.write("\n")

        return xyz
        
    elif heading in range(181, 271):
        south = heading - 180
        west = 270 - heading
        y = (south / -90) * forward_time
        x = (west / -90) * forward_time
        now_time = time.ctime()
        
        xyz = [x, y, now_time]
        
        xyz_save = "xyz_save.json"


        with open(xyz_save, "a") as stamp:
            json.dump(xyz, stamp, indent=0)
            stamp.write("\n")

        return xyz
        
    elif heading in range(271, 361):
        west = heading - 270
        north = 360 - heading
        y = (north / -90) * forward_time
        x = (west / 90) * forward_time
        now_time = time.ctime()
        
        xyz = [x, y, now_time]
        
        xyz_save = "xyz_save.json"


        with open(xyz_save, "a") as stamp:
            json.dump(xyz, stamp, indent=0)
            stamp.write("\n")

        return xyz
 

*One of multiple loads I’ve got. *
*
*
', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0', '1', '7', '"',
'\n', ']', '\n', '[', '\n', '0', '.', '0', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', ',', '\n', '0',
'.', '9', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'7', '7', '7', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J', 'u', 'n', ' ',
'2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0',
'1', '7', '"', '\n', ']', '\n', '[', '\n', '0', '.', '0', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2',
',', '\n', '0', '.', '9', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'8', '8', '8', '8', '8', '9', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J',
'u', 'n', ' ', '2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', '
', '2', '0', '1', '7', '"', '\n', ']', '\n', '\n’]
*
*
*
*
*
*
*the closest i have got is this*
*
*
[
0.07777777777777778,
0.9222222222222223,
"Mon Jun 26 20:37:34 2017"
]
[
0.06666666666666667,
0.9333333333333333,
"Mon Jun 26 20:37:34 2017"
]
[
0.05555555555555555,
0.9444444444444444,
"Mon Jun 26 20:37:34 2017"
]
[
0.044444444444444446,
0.9555555555555556,
"Mon Jun 26 20:37:34 2017"
]
[
0.03333333333333333,
0.9666666666666667,
"Mon Jun 26 20:37:34 2017"
]
[
0.022222222222222223,
0.9777777777777777,
"Mon Jun 26 20:37:34 2017"
]
[
0.011111111111111112,
0.9888888888888889,
"Mon Jun 26 20:37:34 2017”

*with this code*

filename = "xyz_save.json"
with open(filename) as f_obj:
    lines = f_obj.read()
    
print(lines)
*
*
*
*
*
*
*i guess my ultimate question is how do i take all the x and y values
and use them.*
*i apologize for my ignorance as this is my first post about something
very new to me.*
*i also appreciate your patience*
*
*
*
*
*
*





    


> On Jun 27, 2017, at 1:56 AM, Alan Gauld via Tutor <tutor at python.org
> <mailto:tutor at python.org>> wrote:
>
> On 27/06/17 06:32, Micheal Dale Peterson wrote:
>> I am trying to write something that stores x and y data
>> with a time reference and then use it later ..
>
> Lets stick to one thing at a time.
> How far did you get writing the data to a file?
> Did the file get created? Can you edit in the
> text editor?
>
> The simplest way to do it is probably
>
> x = 42
> y = 66
> time = "10:40:22.5"
>
> with open("Myfile.txt") as output:
>    output.write(str(x)+'\n')
>    output.write(str(y)+'\n')
>    output.write(time + '\n')
>
>
> That only outputs one line to the file Myfile.txt,
> but you could put the write lines into a loop if
> you had a list of data items.
>
>> the closest i have come is using the json save.  
>
> Using json is a reasonable approach for this type of problem.
> Can you show us the actual code you used to write the data?
> I assume it did actually get saved to a file in this case?
>
>> Trying to read it back does me no good.
>
> That's too vague to be any use. What does "no good" mean?
> Could you open the file from Python?
> Did you get any data at all?
> Show us some code, even if it doesn't work as you expected.
>
>> i can print each thing individually.
>
> What do you mean? Do you mean you can read each
> item from the file and print it? If so what is
> it that you can't do? Can you save it in a variable?
> Can you add the variable to a list for future use?
>
>> everyday i try i either get a bunch of added characters
>
> Again this this too vague to be helpful.
> Show us what kind of characters.
> Show us the code you are using to read the data and print it.
>
>> or it separates every character.
>
> Again show us code, and sample data so we can see.
> The problem could lie in how you read the file or
> in how you display the data. But without code we can't tell.
>
>> Sorry i can’t  post all of the ways i have tried.
>
> Just post the latest attempts we can start there.
>
>> I am running OS X version 10.12.5 with python 3.6.1.
>
> Thanks for that, it helps.
>
>> my code to get saved list
>
> There's no code...
>
> Did you send it as a binary attachment maybe?
> The list server strips off all binary attachments
> Cut 'n paste the code from your text editor into
> the mail body.
>
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list