Forums

Help needed deciding on variable structure

Waring: Python newbie user question ;-)

I'm trying to use Python to read and interpret text files. I created these files (hundreds of them) over the cause of 6 years, trying to maintain a certain format: line 1: header then separated by 2 blank lines: one or more 'blocks' of text lines

These 'blocks' consist of the following items separated by a single blank line; 0, 1 or multiple lines with filenames 0, 1 or multiple lines with URL's 0, 1 or multiple lines with text

See a sample file below.

I am looking to reuse the contents of these text files, creating a process where I read the contents of a file into memory, then present me with buttons to copy the contents of a (set of) line(s) from a block onto the clipboard.

The problem I'm having is choosing what variable structure is best used to temporarily store the contents of the file in memory since none of these block have fixed content.

Any tips?

Sample file:

----------------------------
header


filename

url

text
text


filename
filename

url

text


filename
filename

url
url

text
text
text


url

text
text

It sounds like you have files that contain blocks, which in turn contain either text, filenames or URLs. Is that right?

I'd suggest something like this (for the data you posted above) for each file:

{
    "file-header": "header", 
    "file-contents": [
        {"type": "filenames", "items": ["filename"]}, 
        {"type": "text", "items": ["text", "text"], 
   ....
}

That is, a dictionary for each file with a value for the header and a value for the list of blocks, then in that list, a dictionary for each block with a value saying what type it is, and a list of the items in it.

A more object-oriented alternative would be to use classes with names like File and Block instead of the dicts, perhaps even with subclasses of Block for FilenameBlock, URLBlock and TextBlock, and some people might prefer doing things that way -- certainly I would have done it that way when I coded in Java a lot, and I can't say it would be wrong to do it that way -- it just seems a little heavyweight to me right now.

OK, wow, you remember I said "newbie user question"? So, trying to wrap my head around this, I will experiment with some same files, first manually converting the into Dictionaries (I guess Notebooks are fine for this). Then working to automatically read the file and process it. It's back to the documentation. Later on I'll also try the OO method a I have some (but limited) OO experience in a VB-like scripting language.

Anyway, thanks a lot for responding.

Let yourself experiment with multiple approaches. You will see what works best for you.