0

I'm having some trouble figuring out the best implementation

I have data in file in this format: |serial #|machine_name|machine_owner|

If a machine_owner has multiple machines, I'd like the machines displayed in a comma separated list in the field. so that.

|1234|Fred Flinstone|mach1|
|5678|Barney Rubble|mach2|
|1313|Barney Rubble|mach3|
|3838|Barney Rubble|mach4|
|1212|Betty Rubble|mach5|

Looks like this:

|Fred Flinstone|mach1|
|Barney Rubble|mach2,mach3,mach4|
|Betty Rubble|mach5|

Any hints on how to approach this would be appreciated.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
maynardg
  • 3
  • 1
  • Why not just split on the `|` character, and then extract the 3 values as `serial_no, machine_name, machine_owner = line.split('|')`? – Alex Huszagh Aug 24 '19 at 20:17
  • You should find your answer here: https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data – PySeeker Aug 24 '19 at 20:24
  • Thanks All for the suggestions. @RoryDaulton: The double pipes was a typo. Thanks for pointing that out. – maynardg Aug 24 '19 at 22:20

2 Answers2

1

You can use dict as temporary container to group by name and then print it in desired format:

import re

s = """|1234|Fred Flinstone|mach1|
|5678|Barney Rubble|mach2|
|1313|Barney Rubble||mach3|
|3838|Barney Rubble||mach4|
|1212|Betty Rubble|mach5|"""

results = {}
for line in s.splitlines():
    _, name, mach = re.split(r"\|+", line.strip("|"))
    if name in results:
        results[name].append(mach)
    else:
        results[name] = [mach]

for name, mach in results.items():
    print(f"|{name}|{','.join(mach)}|")
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
  • Thanks, so much! I didn't think about a list as an element in a dictionary....clever. I'm starting to understand the power of python. – maynardg Aug 24 '19 at 22:18
  • You can accept this answer so that you get 3 rep instead of one. @maynardg – new Q Open Wid Aug 24 '19 at 22:30
  • @maynardg, if double vertical slashes was a typo, you don't need to use regular expressions. You can just use `line.strip("|").split("|")`. – Olvin Roght Aug 25 '19 at 06:32
0

You need to store all the machines names in a list. And every time you want to append a machine name, you run a function to make sure that the name is not already in the list, so that it will not put it again in the list.

After storing them in an array called data. Iterate over the names. And use this function:

data[i] .append( [ ] )

To add a list after each machine name stored in the i'th place.

Once your done, iterate over the names and find them in in the file, then append the owner.

All of this can be done in 2 steps.

moe asal
  • 750
  • 8
  • 24