Python Forum

Full Version: I converted string to 'list', but it doesn't look like a list!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Goal: If I find "FP-X" in the string below, I need to retrieve the string on the left of "FP-X" label.
Like retrieve in this example "0/1/CPU0", "0/5/CPU0" and "5/6/CPU0". I attempted to convert this into a list to let me manipulate elements, but no luck.

string = "
0/1/CPU0      FP-X              40-10GbE           IOS XR RUN      PWR,NSHUT,MON
0/5/CPU0      FP-X              40-10GbE           IOS XR RUN      PWR,NSHUT,MON
0/9/CPU0      FP-140G           14-10GbE           IOS XR RUN      PWR,NSHUT,MON
5/6/CPU0      FP-X              40-10GbE           IOS XR RUN      PWR,NSHUT,MON "

#I confirm this is indeed a 'string' type:
print(type(string))
Output result is:
Output:
<class 'str'>
#Then I attempt to convert into list:
list_data = [string]
 
#I confirm this is now a list:
print(type(list_data)) Output result below is:
Output:
<class 'list'>
#Problem:Then I see the output and I don't see it is converted into a list (there is no '' between elements). 
print(list_data)
Output:
[' platform \r\n\rSat Apr 6 21:38:00.782 EST\r\nNode Type PLIM State Config State\r\n------------- ----------------- ------------------ --------------- ---------------\r\n0/0/CPU0 MSC-B 8-10GbE IOS XR RUN PWR,NSHUT,MON\r\n0/7/CPU0 FP-X 4-100GbE IOS XR RUN PWR,NSHUT,MON\r\n0/RP0/CPU0 RP(Active) N/A IOS XR RUN PWR,NSHUT,MON\r\n0/RP1/CPU0 RP(Standby) N/A IOS XR RUN PWR,NSHUT,MON\r\n1/0/CPU0 MSC-X 40-10GbE IOS XR RUN PWR,NSHUT,MON\r\n1/2/CPU0 MSC-X 40-10GbE IOS XR RUN PWR,NSHUT,MON\r\n1/3/CPU0 MSC Jacket Card IOS XR RUN PWR,NSHUT,MON\r\n1/3/0 MSC(SPA) 10X1GE OK PWR,NSHUT,MON\r\n1/RP0/CPU0 RP(Active) N/A IOS XR RUN PWR,NSHUT,MON\r\n1/RP1/CPU0 RP(Standby) N/A IOS XR RUN PWR,NSHUT,MON\r\nRP/0/RP0/CPU0:CRS-O#']
You probably read this data from file as string provided appears not to be legit multiline string (one should have triple quotes or line ending).

You approach creates list with one element - whole string given.

Regarding your problem: string contains rows, rows contain words. Your objective is to have 'first word in every row'. In order to get that you need split string to rows and then row to words and get first word. This can be expressed in Python as:

>>> s = """0/1/CPU0 FP-X 40-10GbE IOS XR RUN PWR,NSHUT,MON 
...        0/5/CPU0 FP-X 40-10GbE IOS XR RUN PWR,NSHUT,MON 
...        0/9/CPU0 FP-140G 14-10GbE IOS XR RUN PWR,NSHUT,MON 
...        5/6/CPU0 FP-X 40-10GbE IOS XR RUN PWR,NSHUT,MON""" 
>>> [row.split()[0] for row in s.split('\n')] 
['0/1/CPU0', '0/5/CPU0', '0/9/CPU0', '5/6/CPU0']
I missed requirement that there must be 'FP-X' in row. This condition can be easily added:

>>> [row.split()[0] for row in s.split('\n') if row.split()[1] == 'FP-X']
['0/1/CPU0', '0/5/CPU0', '5/6/CPU0']
The data is multiline data, you can split on the newline character to retrieve a list of lines
my_list = my_string.split('\n')
Thank you. the split ('\n') was enough. Appreciated.