Reputation: 3
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.
Upvotes: 0
Views: 59
Reputation: 7812
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)}|")
Upvotes: 1
Reputation: 582
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.
Upvotes: 0