Chase Garfield - 1 year ago 58

Python Question

So I have a bit of code that looks like this:

`measure_list=[]`

for file in file_list:

with open(file,"r") as read_data:

y=read_data.read()

if "Part: ABCD" in y:

for line in m:

if "Measure X:" in line:

measure_list.append(line)

elif "Measure Y:" in line:

measure_list.append(line)

final=''.join(measure_list).replace("Measure","\nMeasure")

print(final)

(This last part just helps organize the output)

So this part of the code opens a group of files and scans each one to see if it is "Part: ABCD", and if it is, it will pull the lines for "Measure X:" and "Measure Y:" and add them to the measure_list. My problem is that I have a lot of files and there will be multiple "Part: ABCD" files. And after joining the list with .join() the output will then look like:

`Part: ABCD`

Measure X: (Numbers)

Measure Y: (Numbers)

Measure X: (Numbers)

Measure Y: (Numbers)

Measure X: (Numbers)

Measure Y: (Numbers)

My question is if anybody knows a way to organize the output so that it looks like so:

`Part: ABCD`

Measure X: (Numbers)

Measure X: (Numbers)

Measure X: (Numbers)

Measure Y: (Numbers)

Measure Y: (Numbers)

Measure Y: (Numbers)

Any help is appreciated.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Your `measure_list`

seems like this:

```
['Measure X: 1000', 'Measure Y: 2000', 'Measure X: 100' , 'Measure Y: 900']
```

If that's right, you can simply sort this `measure_list`

:

`measure_list.sort()`

Then you will have:

```
['Measure X: 100', 'Measure X: 1000', 'Measure Y: 2000', 'Measure Y: 900']
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**