Python Forum
I converted string to 'list', but it doesn't look like a list!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I converted string to 'list', but it doesn't look like a list!
#1
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#']
Reply
#2
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']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
The data is multiline data, you can split on the newline character to retrieve a list of lines
my_list = my_string.split('\n')
Reply
#4
Thank you. the split ('\n') was enough. Appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  Why doesn't list require global keyword? johnywhy 9 795 Jan-15-2024, 11:47 PM
Last Post: sgrey
  Sample random, unique string pairs from a list without repetitions walterwhite 1 448 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  trouble reading string/module from excel as a list popular_dog 0 416 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,156 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  String to List question help James_Thomas 6 975 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 May-01-2023, 09:06 PM
Last Post: deanhystad
  Converted EXE file size is too large Rajasekaran 0 1,506 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  List all possibilities of a nested-list by flattened lists sparkt 1 914 Feb-23-2023, 02:21 PM
Last Post: sparkt
  convert string to float in list jacklee26 6 1,897 Feb-13-2023, 01:14 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020