Extract lines from file, add to new files

dn PythonList at DancesWithMice.info
Mon Jan 29 14:10:48 EST 2024


On 30/01/24 05:15, Rich Shepard via Python-list wrote:
> On Fri, 12 Jan 2024, Rich Shepard via Python-list wrote:
> 
>> For my use 1) the salutation and email address (always with an '@') are
>> sequential and 2) I'm developing the script to extract both from the same
>> file.
> 
> I've looked at my Python books "Python Crash Course," "Effective Python,"
> and "Python Tricks The Book" as well as web pages in my searches without
> finding the answer to what may be a simple question: how to specify a
> variable in one file that has its values in another file.
> 
> Specifically, how to I designate the salutation holder in the message file
> and pass it the name value from the name/email address file?
> 
> If this explanation is not sufficiently clear I'll re-write it. :-)


It seems clear - but maybe we (all) misunderstand?

The books don't cover it - but is there a reason why they don't?


(Thunderbird did not thread this message with earlier ones, and it seems 
some time has passed/memory is fading - but hopefully have remembered 
content)


As said previously, the idea of two physical-files containing 
logically-related data (without any form of cross-reference between) is 
bad design.

In the current file, there is one physical structure and each person's 
details are logically-related by proximity. A long way from ideal, but 
workable (as you've described).

However, once split into two files, there is no way to guarantee that 
the two logically-related data-items (name and address) will continue to 
be related by their physical position in the respective files. Worse: 
whilst it would seem apparent that "Alice" from the names file might be 
related to the address "alice at domain.tld", how could one know if "Bob" 
actually corresponds to "list-member at domain.tld"?

This is why dicts, databases, etc, offer keys (as labels for 
data-items/dependent components)!


After a quick look at Eric's Crash Course, yes, his files-intro example 
(digits of pi) is unlikely to have any practical reality (and I work 
with statisticians and quants!). However, at the end of that chapter 
(10), there is mention of JSON files. A JSON version of the existing 
single-file structure will provide human-readable labeling of 
data-items, give better separation between individuals' data, and show 
how name and address are linked. Recommend solving 'the problem' that 
way! (as previously discussed by others 'here', IIRC)

Slatkin's Effective Python doesn't seem to discuss the basics of files 
at all (presumes such knowledge of the reader). It dives into important, 
but rather technical discussions, comparing strings and bytes - somewhat 
beyond the complexity-level of this discussion. That book does however 
include discussions such as "Prefer Multiple Assignment Unpacking over 
Indexing" (Item 6 - also points to Item 19) where relative-positioning 
(indexing in other words) is advised-against.


If you wish to persist with this two-file structure, please see earlier 
responses (again, IIRC) and discussion of file-merge operations. As 
these date back to mainframes and the days of storing files on mag-tape, 
I'd be surprised to see them discussed in 'modern' texts. However, the 
principle is: read a record from each file, do-the-business, read the 
next 'pair' of physically-related records, rinse-and-repeat.

If you require further assistance: how about showing a couple of 
relevant lines of the data-file(s) and the pertinent parts of the code, 
along with a description of what's going-wrong or not making sense to you?

-- 
Regards,
=dn


More information about the Python-list mailing list