-1

I just wanted to query the best way to read the lines of a text file back into a dict or class object.

The data I've written to a text file is quite crude, so I'm sure there may be better ways of formatting.

Essentially the data looks like this (-- is my comments):

Default_scene --this is the scene name 
0 -- start frame
10 --end frame
--this could be a filepath but is blank
2 --number of records in class object can vary but 2 here
Dave01: --record name 
Dave01: -- namespace
V:/assets/test.fbx --filepath for record
1 -- number of export sets (can vary)
Test:Default_scene_Dave01 0-10 --export set
Test01: --record name
Test01:--namespace
C:/test2.fbx --filepath
0--number of export sets

For instance: if I wanted to read a records data back into an object class, how would i tell the read lines script to read the next 1 or even 2 lines depending on how many export sets there might be?

Many thanks!

rdas
  • 20,604
  • 6
  • 33
  • 46
TeXaS_pEtE
  • 59
  • 4
  • 2
    Is there any reason you couldn't just open the file and read all of the lines into a list and then process them however you see fit from there? – zerocool May 16 '19 at 16:57
  • 4
    Also, once you've opened a file with `f = open('filename.txt')`, you can do `f.readline()` to read one line at a time - or you can iterate through `f` in a `for` loop to read each line successively. – Green Cloak Guy May 16 '19 at 16:59
  • There are many tutorials and examples on reading a text file. We hold you responsible for making a good-faith attempt before posting here. – Prune May 16 '19 at 17:08
  • Sorry, I'm away from my machine at present & it was a question that's been bugging me. I'll post up some code if i run into any issues. Cheers. – TeXaS_pEtE May 16 '19 at 17:19

2 Answers2

1

Maybe something like this could work, then you'd have everything in a list and can work with it:

lines = []
with open(filepath) as fp: 
   line = fp.readline()
   lines.append(line)
   while line:
       line = fp.readline()
       lines.append(line)
Ahmad Moussa
  • 876
  • 10
  • 31
  • 2
    simply use `for line in fp:` is the normal pattern and it is 1 line against your 5 .. if you want to get each line into memory do `lines = fp.readlines()` – Patrick Artner May 16 '19 at 17:04
  • 1
    How is this different from just calling `open(filename).readlines()` or even just `list(open(filename))`. In any case I don't think this is what OP is asking, though that's very vague as well and we may never know... – Iguananaut May 16 '19 at 17:04
  • I see. The problem I've got is that everything from the record name to the (potential) export set names are associated to the record. What's confusing me is how I'd make sure it read the correct record details back in, especially if one record had 2 export sets and one had none. – TeXaS_pEtE May 16 '19 at 17:05
1

You read the line that tells you how many export sets there are first, then read that many more lines to process. Code based on your comments might look like

with open("data.txt") as f:
    scene_name = next(f).strip()
    start_frame = int(next(f))
    end_frame = int(next(f))
    num_recs = int(next(f))
    for _ in range(num_recs):
        rec_name = next(f).strip()
        namespace = next(f).strip()
        fpath = next(f).strip()
        num_export_sets = int(next(f))
        export_sets = [next(f).strip() for _ in range(num_export_sets)]
        # ...
chepner
  • 497,756
  • 71
  • 530
  • 681