1

I have developed a program to read through text in files and capture information, and write it into a CSV.

Each "field" is identified by an index, a label, and a value, like so:

.0 index
.0.label unicode "Area"
.0.value unicode "6WAY DB"
.1 index
.1.label unicode "SubStation"
.1.value unicode "E782DB257"
.2 index
.2.label unicode "MCC ID"
.2.value unicode ""
.3 index
.3.label unicode "Feeder"
.3.value unicode ""
etc...

In this CSV, I capture the "label" as a column header, then the "value" as the data for each record - simple enough. However, the situation begins to get complex when some files have different information - different labels especially.

Creating the header row isn't such a difficult thing, but making sure the data is captured correctly is a different matter. For example,

File A:

.0 index
.0.label unicode "Area"
.0.value unicode "6WAY DB"

File B:

.0 index
.0.label unicode "SubStation"
.0.value unicode "110"

That means I need to somehow identify that even though these are the first items in each file, I need to identify these as separate fields. I was thinking of using index tags as a part of building the column header row, or even using the labels as tags for each field, so that they can be entered in the correct order?

For example, I would build the column header like so:

[0: Area, 1: Equipment, …]

Then when reading through the file, use that to identify the appropriate location for each field

filename | Area    | SubStation | Other
File A   | 6WAY DB | E782DB257  |
File B   |         | 110        |
etc...

Can/should I use arrays for this? or is there a batter way to capture this information?

  • 1
    Since you are the one writing this CSV, might I suggest that your problem would be easier to solve if you wrote it using a better data format? The format that you've illustrated in your last example would be much easier to read, if you just wrote it that way from the start. – Robert Harvey May 24 '18 at 5:24
  • 2
    You can have a dictionary with a string-based field name as key, and the dictionary's value is another dictionary. You can also have a dictionary whose key is a composite, i.e. the key itself consists of a tuple, and its GetHashCode and Equals methods have been overridden to take into account each of the tuple's components. – rwong May 24 '18 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.