Python Forum
How to load log.txt directly into python codes?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to load log.txt directly into python codes?
#1
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.
Reply
#2
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
Reply
#3
(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!
Reply
#4
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
Reply
#5
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.
Reply
#6
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.
Reply
#7
I got it! Thank you for your thorough explanation, this alone also saves me lots of time!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 394 Oct-31-2023, 10:43 AM
Last Post: piyushd
  Import XML file directly into Excel spreadsheet demdej 0 800 Jan-24-2023, 02:48 PM
Last Post: demdej
  How to set LD_LIBRARY_PATH on python codes? aupres 1 8,328 Mar-25-2021, 08:49 AM
Last Post: buran
  Load table from Oracle to MYSQL using python himupant94 0 1,608 May-12-2020, 04:50 PM
Last Post: himupant94
  Get input directly as a number? Pedroski55 4 2,152 May-05-2020, 04:29 PM
Last Post: deanhystad
  Python - help with getting JSON from one DB and load to another DB qIekm 4 3,179 Apr-16-2020, 07:07 AM
Last Post: qIekm
  TypeError indexing a range of elements directly on the list JFerreira 2 2,167 Mar-30-2020, 04:22 PM
Last Post: bowlofred
  problem with mapnik in anaconda python 2: from _mapnik import * ImportError: DLL load parsley 0 1,873 Dec-11-2019, 07:50 AM
Last Post: parsley
  python and load balancer pythonFresher 1 2,245 Jul-18-2019, 07:23 AM
Last Post: pythonFresher
  Fatal Python error: Py_Initialize: unable to load the file system codec ecg1g15 0 3,535 Feb-12-2019, 12:16 PM
Last Post: ecg1g15

Forum Jump:

User Panel Messages

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