Python Forum
How to load log.txt directly into python codes? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to load log.txt directly into python codes? (/thread-29085.html)



How to load log.txt directly into python codes? - sparkt - Aug-18-2020

I wrote a very simple program which can retrieve some data I need. It's then written to a txt file. It looks like the following.

Tencentlh =  ([0.675000011920929, 0.8550000190734863, 1.6699999570846558, 4.699999809265137, 6.800000190734863, 17.5, 24.079999923706055, 27.959999084472656, 30.059999465942383, 47.400001525878906, 93.0, 111.30000305175781, 132.10000610351562, 188.0, 251.39999389648438, 300.3999938964844, 325.20001220703125], [1.2699999809265137, 1.940000057220459, 5.550000190734863, 14.5600004196167, 14.40999984741211, 33.08000183105469, 38.599998474121094, 46.15999984741211, 56.20000076293945, 100.4000015258789, 134.89999389648438, 171.0, 220.8000030517578, 439.6000061035156, 476.6000061035156, 400.3999938964844, 564.0])
Then I need to do further statistics. It would be great if I can just directly load the txt file as python codes, a bit like import a function from a module, so I can directly reuse the value "Tencentlh" and of course all the other values.
How can I do that? Any help would be much appreciated.


RE: How to load log.txt directly into python codes? - Larz60+ - Aug-18-2020

why not start with the data already in a text or json file?
then it's a simple read() or json.load operation to import.

here's an example that creates the json file, then reads it back:
import json
from pathlib import Path
import os

Tencentlh =  [[0.675000011920929, 0.8550000190734863, 1.6699999570846558, 4.699999809265137, 6.800000190734863, 17.5, 24.079999923706055, 27.959999084472656, 30.059999465942383, 47.400001525878906, 93.0, 111.30000305175781, 132.10000610351562, 188.0, 251.39999389648438, 300.3999938964844, 325.20001220703125], [1.2699999809265137, 1.940000057220459, 5.550000190734863, 14.5600004196167, 14.40999984741211, 33.08000183105469, 38.599998474121094, 46.15999984741211, 56.20000076293945, 100.4000015258789, 134.89999389648438, 171.0, 220.8000030517578, 439.6000061035156, 476.6000061035156, 400.3999938964844, 564.0]]

os.chdir(os.path.abspath(os.path.dirname(__file__)))
jsonfile = Path('.') / 'Tencentlh.json'

with jsonfile.open('w') as fp:
    json.dump(Tencentlh, fp)

def test_load():
    with jsonfile.open() as fp:
        newdata = json.load(fp)
        print(f"\nnewdata list1: {newdata[0]}")
        print(f"\nnewdata list2: {newdata[1]}")
        print(f"\n3rd element of list 2: {newdata[1][2]}")

test_load()
output:
Output:
newdata list1: [0.675000011920929, 0.8550000190734863, 1.6699999570846558, 4.699999809265137, 6.800000190734863, 17.5, 24.079999923706055, 27.959999084472656, 30.059999465942383, 47.400001525878906, 93.0, 111.30000305175781, 132.10000610351562, 188.0, 251.39999389648438, 300.3999938964844, 325.20001220703125] newdata list2: [1.2699999809265137, 1.940000057220459, 5.550000190734863, 14.5600004196167, 14.40999984741211, 33.08000183105469, 38.599998474121094, 46.15999984741211, 56.20000076293945, 100.4000015258789, 134.89999389648438, 171.0, 220.8000030517578, 439.6000061035156, 476.6000061035156, 400.3999938964844, 564.0] 3rd element of list 2: 5.550000190734863



RE: How to load log.txt directly into python codes? - sparkt - Aug-19-2020

(Aug-18-2020, 04:20 PM)Larz60+ Wrote: why not start with the data already in a text or json file?
then it's a simple read() or json.load operation to import.

I think that's exactly what I'm looking for. I'll study more about json. Thanks very much!


RE: How to load log.txt directly into python codes? - Larz60+ - Aug-20-2020

Please note the test_load function.
This is the method toy would use in your programs to load the data in.
This requires an import at top of program: import json


RE: How to load log.txt directly into python codes? - sparkt - Aug-20-2020

OK thanks. Not so complicated about the load part but seems to me more complicated about the os part. Might go through it if I need, now seems the json part is good for me enough.


RE: How to load log.txt directly into python codes? - Larz60+ - Aug-20-2020

the OS part is simple.

It has nothing to do with the remainder of the code, other than it's sole purpose, which is to guarantee that the starting directory is the same as where the python script is located.

This is where I wanted the json file to be located, you don't need it if you are saving the file elsewhere,
but is doesn't hurt, and gives you a starting anchor for all file operations.

I include this in all of my code, it has saved me hours of trying to figure out why files aren't where I wanted them to be.


RE: How to load log.txt directly into python codes? - sparkt - Aug-21-2020

I got it! Thank you for your thorough explanation, this alone also saves me lots of time!