Python Forum

Full Version: import/use data from text files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey all. This is my first post. I have heard how awesome the Python community is and I am dying to find out.
I have been stuck on this for about a week. So that being said I would not bug you all unless it was a last resort.

Here is my struggle

I am trying to store x and y information along with a "time stamp"  to a text file or json. Later I would love to use this information to create a graph. I succeeded in researching how to make a graph. And, for the most part have succeeded in storing information.  I however am extremely confused about how to load that information and use it. The few tutorials and the book i bought show me how to do this with one piece of information. Not multiple bits of info and not list, dictionaries, etc. in fact even reading through tutorials this is the majority of what confuses me about this brand new world of coding.

My questions

#1. am i storing this information in a correct way to use later

#2. How on earth do i load this info and use it. Specifically take the full x information separately and y separately
and use them in other parts of my code like adding them together. combining them with other info and using them for points on a graph etc. I don't necessarily have a use for the time stamp as of now but I'm assuming answering the one will answer the other.

#3. there is a section of my code where i index data[0:1] and save that as a. then reference a[0:1]. they end up outputting the same thing. in theory shouldn't referencing a[0:1] print out the first character of data[0:1]

I apologize in advance if my code is a little all over the place.  I have been at this for a week trying everything i can thing of and have cut, copied, commented out. print to it seems like a thousand files


this is my code that creates the save file. I am only showing a part of it as it is the same thing repeated multiple times with different integers. if the rest is needed let me know

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)
            stamp.write("\n")

        return xyz
this is the save file. i have tried this with a text file this is the closet i got to the result i think i need
the first set of numbers is the x, the second is the y and the last obviously time

Output:
[0.9888888888888889, 0.011111111111111112, "Tue Jun 27 22:06:50 2017"] [0.9777777777777777, 0.022222222222222223, "Tue Jun 27 22:06:50 2017"] [0.9666666666666667, 0.03333333333333333, "Tue Jun 27 22:06:50 2017"] [0.9555555555555556, 0.044444444444444446, "Tue Jun 27 22:06:50 2017"] [0.9444444444444444, 0.05555555555555555, "Tue Jun 27 22:06:50 2017"] [0.9333333333333333, 0.06666666666666667, "Tue Jun 27 22:06:50 2017"] [0.9222222222222223, 0.07777777777777778, "Tue Jun 27 22:06:50 2017"] [0.9111111111111111, 0.08888888888888889, "Tue Jun 27 22:06:50 2017"] [0.9, 0.1, "Tue Jun 27 22:06:50 2017"]
this is the code i am trying a thousand different ways to get the result. it has been changed so much. but this is still the closet i can get to the result I'm trying to get

filename = "xyz_save.json"
with open(filename) as f_obj:
    lines = f_obj.readlines()

##print(lines[0:1])
    data = []

    for line in lines:
        line.strip()
##        line.split()
##        data.append(line)
        data.append(line[1:-3])

    print(data[0:1])
    a = data[0:1]
    print(a[0:1])
this is the result

Output:
== RESTART: /Users/petmi/Desktop/Python Practice/MAP PLOT/xyz_run_update.py == ['0.9888888888888889, 0.011111111111111112, "Tue Jun 27 22:06:50 2017'] ['0.9888888888888889, 0.011111111111111112, "Tue Jun 27 22:06:50 2017'] >>>
thank you all. any guidance would be appreciated
One way would be to do all the work yourself:
1. read a line (from the csv file or the json object)
2. strip the new line '\n' (if reading from text file)
3. split at the separator ',' (if reading from text file
4. convert to float using float()

steps 2 and 3 can be done for you if using the csv module (part of the Standard Python library)

all of the above could be done also using external packages as pandas/numpy. Given that you plan to plot the data - maybe this is the way to go.