On Wed, 22 Sep 2021 09:52:59 -0500, "Michael F. Stemper" <michael.stem...@gmail.com> declaimed the following:
>On 21/09/2021 19.30, Eli the Bearded wrote: >> In comp.lang.python, Michael F. Stemper <michael.stem...@gmail.com> wrote: >>> How does CSV handle hierarchical data? For instance, I have >>> generators[1], each of which has a name, a fuel and one or more >>> incremental heat rate curves. Each fuel has a name, UOM, heat content, >>> and price. Each incremental cost curve has a name, and a series of >>> ordered pairs (representing a piecewise linear curve). >>> >>> Can CSV files model this sort of situation? >> <SNIP> >> Yes, CSV files can model that. But it would not be my first choice of >> data format. (Neither would JSON.) I'd probably use XML. > >Okay. 'Go not to the elves for counsel, for they will say both no >and yes.' (I'm not actually surprised to find differences of opinion.) > You'd have to include a "level" (and/or data type if multiple objects can be at the same level) field (as the first field) in CSV which identifies how to parse the rest of the CSV data (well, technically, the CSV module has "parsed" it -- in terms of splitting at commas, handling quoted strings (which may contain commas which are not split points, etc.). 1-generator, name 2-fuel, name, UOM, heat-content, price 2-curve, name 3-point, X, Y 3-point, X, Y ... 2-curve, name 3-point, X, Y 3-point, X, Y ... You extract objects at each level; if the level is the same or "lower" (numerically -- higher in hierarchy) you attach the "previously" extracted object to the parent object... Whether list or dictionary, or class instance(s): class Point(): #Point may be overkill, easier to just use a tuple (X, Y) def __init__(self, X, Y): self.X = X self.Y = Y class Curve(): def __init__(self, name): self.name = name self.points = [] #use as aCurve.points.append(currentPoint) class Fuel(): def __init__(self, name, ..., price): self.name = name ... self.price = price class Generator(): def __init__(self, name): self.name = name self.fuel = None self.curves = [] #aGenerator.fuel = currentCurve #aGenerator.curves.append(currentCurve) -- Wulfraed Dennis Lee Bieber AF6VN wlfr...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list