0

I got a text file containing lines separated in this manner:

Action: Add Parameter
Matched Parameter: ctl00_ContentPlaceHolderMain_RadSearchBoxNeId_ClientState 
on [HTTPS] /ConsolePage/ConsolePageWeb.aspx Matched Wildcard: * 

Action: Add Parameter
Matched Parameter: ctl00$ContentPlaceHolderMain$HiddenFieldSelectedFilter on 
[HTTPS] /ConsolePage/ConsolePageWeb.aspx Matched Wildcard: * 

I've written a small script in Python to get only the string after the "Matched Paramater: " and output it to a file, however the results are not sorted properly.

The script:

import re

pattern = "^Matched Parameter: ([^\s]+)"
new_file = []

with open(".\params.txt") as txtFile:
    lines = txtFile.readlines()

for line in lines:
    match = re.search(pattern, line)
    if match:
        new_line = match.group()
       new_line = new_line.split(" ")
    del new_line[0], new_line[0]
    new_line = sorted(new_line)
    print(new_line)

Output:

['ctl00_MainSplitter_ClientState']
['ctl00_RadWindowLicenseAggreemennt_C_RadButtonLicenseAggreemenntCancel_ClientState']
['ctl00_RadWindowLicenseAggreemennt_C_RadButtonLicenseAggreemenntOK_ClientState']
['ctl00_RadWindowLicenseAggreemennt_ClientState']
['ctl00$ScriptManagerMain']
['ctl00_RadStyleSheetManager1_TSSM']
['ctl00_ScriptManagerMain_TSM']
['__VIEWSTATE']
['ctl00_radwindow1_ClientState']
['ctl00_RadButtonLgout_ClientState']
['ctl00_TopPane_ClientState']
['ctl00_RadPanelBarMainMenu_ClientState']
['ctl00_LeftPane_ClientState']
['ctl00_ContentPlaceHolderMain_RadWindowManager1_ClientState']
['ctl00$ContentPlaceHolderMain$RadComboBoxTimeResolution']
['ctl00_ContentPlaceHolderMain_RadComboBoxTimeResolution_ClientState']
['ctl00_ContentPlaceHolderMain_RadSearchBoxNeId']
['ctl00_ContentPlaceHolderMain_RadSearchBoxNeId_ClientState']
['ctl00$ContentPlaceHolderMain$HiddenFieldSelectedFilter']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxRuleNames_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxSeverity_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxStatus_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxEntityType_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterOK_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadListBoxExistingFilters_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListOk_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowFiltersList_C_RadButtonFiltersListEdit_ClientState']

I need the output to be sorted by it's parameter name substring in alphabetical order, for example 'AlarmsUserFilters' before 'ClientState':

['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadButtonAlarmsFilterClose_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxRuleNames_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxSeverity_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxStatus_ClientState']
['ctl00_ContentPlaceHolderMain_AlarmsUserFilters_RadWindowAlarmsFilter_C_RadListBoxEntityType_ClientState']
['ctl00_MainSplitter_ClientState']

Any help how to do this in the best possible way?, I need to be generic as possible, i.e there could be different strings that need to be sorted this way('ct100' etc.. is just one example)

Thanks!

5
  • It seems to be throwing a NameError.. Should the lines after the if be indented? Commented Aug 21, 2017 at 9:38
  • You are not sorting anything. Your for loop goes through your data and prints out results in exactly the same order as they were present in the input file; new_line that you pass to sorted is always just a single-element list. Commented Aug 21, 2017 at 9:38
  • I need an answer which is generic, splitting by '_' is good for this use case, but in the future I'll have strings which can look different. I need something that sorts substrings alphabretically in a general way Commented Aug 21, 2017 at 9:40
  • @IzaakvanDongen Yeah it should be indented, sorry something screwed up when I pasted in in the site's question editor Commented Aug 21, 2017 at 9:43
  • this will sort it simply by characters sorted(lines, key=lambda val: val[0]) Commented Aug 21, 2017 at 9:44

1 Answer 1

1

As others have pointed out, the problem is that you're only building a list of length 1, sorting it then immediately printing it before reading the whole file. I've changed your code to this:

import re

pattern = "Matched Parameter: ([^\s]*)"
parameters = []

with open(".\\params.txt") as txtFile:
    for line in txtFile:
        match = re.match(pattern, line)
        if match:
            parameters.append(match.group(1))

for par in sorted(parameters):
    print(par)

It should now work fine. This has also changed some other bits and pieces a bit - match.group(1) just immediately gets you the matched group, which is the bit in parentheses () in your regex. Also, as you only want to match from the start of the line, you can use re.match. I'm also iterating over the lines of the file directly, rather than building up the lines and then iterating over those. Note that you were correct in that it is entirely possible to use sort and sorted on a list of strings, as Python compares them 'alphabetically', which is often called 'lexicographically'.

Unfortunately I might struggle to provide any meaningful sample output without spending ages reconstructing your input, as I don't have access to it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.