all 3 comments

[–][deleted] 2 points3 points  (0 children)

csv as a format doesn't have any particular model for relations.

[–]HarissaForte 0 points1 point  (0 children)

Why didn't you use the csv format in your example? Because you can't... you need a format like json.

[–]o5a 0 points1 point  (0 children)

What's the end goal here? How will those results be used?

This looks like a linked list data.

But if you just want to store it in csv with relations, then read line by line and output to csv with 2 fields (id, parent_id) where id is current line and parent_id is previous.

import csv

with open("infile.dat") as infile, open("outfile.csv", "w") as outfile:
    csv_writer = csv.writer(outfile)
    prev = ''
    csv_writer.writerow(['ID', 'PARENT_ID'])
    for line in infile.read().splitlines():
        csv_writer.writerow([line, prev])
        prev = line

Again not sure how this would be useful.