Python Forum
import/use data from text files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import/use data from text files
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import multiple CSV files into pandas Krayna 0 1,695 May-20-2021, 04:56 PM
Last Post: Krayna
  import columns of data from local csv file CatherineKan 2 3,302 May-10-2021, 05:10 AM
Last Post: ricslato
  Most Compatible Text Editor to Handle Large Files? Robotguy 2 2,338 Aug-18-2020, 03:51 PM
Last Post: FortyTwo
  Binning data to files Kappel 4 2,368 Jun-22-2020, 06:25 PM
Last Post: Kappel
  Unable to import data from Stata-press website Salahaddin 1 2,213 Jun-08-2020, 04:23 AM
Last Post: buran
  Can python read Marathi text files and summarize them? mcp111 0 1,784 Mar-18-2020, 08:58 AM
Last Post: mcp111
  Ask for machine learning Python example with 2 data files user5566b 2 2,232 Sep-05-2019, 12:15 PM
Last Post: user5566b
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 5,989 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  import numpy in sub-files paul18fr 1 1,987 Aug-06-2019, 12:38 PM
Last Post: chakrimakam
  Text files vretenica 5 2,953 Jul-03-2019, 03:24 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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