2

I'm building a Python script to do a network inventory on our internal system. Each system has up to five clusters and I am looking for a way to adjust the list to put them all into one line per cluster. Here's the way they are stored on the system:

Cluster One
Primary System
Node One
x.x.x.x (Node IP)
Node two
x.x.x.x (Node IP)
Cluster Two
Primary System
Node One
x.x.x.x (Node IP)
Node two
x.x.x.x (Node IP)

I need to have them adjusted to look like this:

Cluster One,Primary System,Node One,x.x.x.x (Node IP),Node Two,x.x.x.x (Node IP)
Cluster Two,Primary System,Node One,x.x.x.x (Node IP),Node Two,x.x.x.x (Node IP)

Here is some REALLY sloppy code I'm using right now, but I was wondering if there is a more efficient way to do it:

NetworkTableCount = len(NetworkTable) / 6 + 1
    count = 1
    while count < NetworkTableCount:
            if count == 1:
                    Temp = (NetworkTable[0]+","+NetworkTable[1]+","+NetworkTable[2]+","+NetworkTable[3]+","+NetworkTable[4]+","+NetworkTable[5]+"\n")
            elif count == 2:
                    Temp = (NetworkTable[6]+","+NetworkTable[7]+","+NetworkTable[8]+","+NetworkTable[9]+","+NetworkTable[10]+","+NetworkTable[11]+"\n")
            elif count == 3:
                    Temp = (NetworkTable[12]+","+NetworkTable[13]+","+NetworkTable[14]+","+NetworkTable[15]+","+NetworkTable[16]+","+NetworkTable[17]+"\n")
            elif count == 4:
                    Temp = (NetworkTable[18]+","+NetworkTable[19]+","+NetworkTable[20]+","+NetworkTable[21]+","+NetworkTable[22]+","+NetworkTable[23]+"\n")
            elif count == 5:
                    Temp = (NetworkTable[24]+","+NetworkTable[25]+","+NetworkTable[26]+","+NetworkTable[27]+","+NetworkTable[28]+","+NetworkTable[29]+"\n")
            NetworkTopology.write(Temp)
            count = count + 1

I'm self taught in Python so it may just be a simple adjustment. Thanks in advance.

Taylor
  • 510
  • 2
  • 5
  • 16
  • 1
    You should first refactor your code to simplificate the computation of `Temp`. There is enough ressemblance in these lines to generalise them into one easier to read line. Consider using something like this: `index = (count - 1) * 6; ",".join(NetworkTable[index:index+6])` – Lynch Sep 06 '11 at 18:43

2 Answers2

2

First look: How do you split a list into evenly sized chunks?

Using one of the answers:

>>> import itertools
>>> NetworkTable
['Cluster One', 'Primary System', 'Node One', 'x.x.x.x (Node IP)', 'Node two', 'x.x.x.x (Node IP)', 'Cluster Two', 'Primary System', 'Node One', 'x.x.x.x (Node IP)', 'Node two', 'x.x.x.x (Node IP)']
>>> l = [NetworkTable[i:i+6] for i in range(0, len(NetworkTable), 6)]
>>> l
[['Cluster One', 'Primary System', 'Node One', 'x.x.x.x (Node IP)', 'Node two', 'x.x.x.x (Node IP)'], ['Cluster Two', 'Primary System', 'Node One', 'x.x.x.x (Node IP)', 'Node two', 'x.x.x.x (Node IP)']]
>>> #Now it's easy. Do what you want with l.
>>> [", ".join(i) + "\n" for i in l]
['Cluster One, Primary System, Node One, x.x.x.x (Node IP), Node two, x.x.x.x (Node IP)\n', 'Cluster Two, Primary System, Node One, x.x.x.x (Node IP), Node two, x.x.x.x (Node IP)\n']
Community
  • 1
  • 1
utdemir
  • 26,532
  • 10
  • 62
  • 81
1

I would proces the lines based on the name they begin with, as it seems a 'Cluster' specifies a new set of data:

file = open('my_file')
clusters=[]
line = file.readline()
while line.strip().startswith("Cluster"):
        cluster = []
        cluster.append(line.strip())
        line = file.readline()
        cluster.append(line.strip())
        line = file.readline()
        while line.strip().startswith("Node"):
                node = []
                node.append(line.strip())
                line = file.readline()
                node.append(line.strip())
                cluster.append(node)
                line = file.readline()
        clusters.append(cluster)
# now you have a list of clusters, where nodes are in a list too
print(clusters[0])
print(clusters[1])
steabert
  • 6,540
  • 2
  • 26
  • 32
  • Thank you for the answer. Unfortunately it's not ALWAYS called "Cluster". It's based on how the system is configured, but about 80% DO start with "Cluster". No matter what though, it'll always be in sets of six. – Taylor Sep 09 '11 at 13:02