1

I am trying to create several lists by breaking up each list entry. the format is as follows:

LIST ENTRY FORMAT

['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']

basically, I have many entries in my list that follow this format and I need to break up each entry into several lists, starting a new list at each \n symbol. such as this:

list1 = root cause: hardware failure list2 = action completed: power supply/filter/cable swap

and so on and this will need to do this for the thousands of entries so i have a list of all root causes a list of all actions completed and so and so on

I have managed to put /n symbol at each point I want a new list starting but not sure where to go from there

any help is appreciated

ipmev12
  • 77
  • 10

5 Answers5

3

Use a list comp and str.splitlines to split every string in the lists into sublists of individual lines:

split_lines = [s.splitlines() for s in lst]

If you want each individual string inside a list:

  from itertools import chain

  split_lines = [[s] for s in chain(*map(str.splitlines, lst))]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

You can use the split() method to create the items and then place them in a master list like so:

alist = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']

new_lists = []
for i in alist:
    for j in i.split('\n'):
        new_lists.append([j])

print(new_lists)
# [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap'], ...

The first for loop is in case your initial list contains more than one entries that have to be split.

Ma0
  • 15,057
  • 4
  • 35
  • 65
0

Is this what you're looking at??

x = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap\nArrival Time: 02/01/2014 15:54:17\nLeaving Time: 02/01/2014 16:27:44\nWas the job successful: Yes\nNotes:replaced dsl cable and filter. Also rebooted all equipment. All working fine now.\nNext Action required:none\nAdded by jakubkwasny at 02/01/2014 21:41:40\nPinging 55:55:55:55 with 32 bytes of data:\nReply from 55:55:55:55: bytes=32 time=67ms TTL=240\nReply from 55:55:55:55: bytes=32 time=92ms TTL=240\nReply from 55:55:55:55: bytes=32 time=76ms TTL=240\nReply from 55:55:55:55: bytes=32 time=82ms TTL=240\nPing statistics for 88.55.55.55:\nPackets: Sent = 4, Received = 4, Lost = 0 (0% loss),\nApproximate round trip times in milli-seconds:\nMinimum = 67ms, Maximum = 92ms, Average = 79ms']


y = x[0].split('\n')
harshil9968
  • 3,254
  • 1
  • 16
  • 26
0

if you want list of lists:

list(map(lambda s: [s], old_list[0].splitlines()))

you can read more about lambda functions and map builtin

Community
  • 1
  • 1
Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • in python 3 `map` does not create a list. You need to pass it to the `list()` function like `list(map(...))`. Please update since Python 3 is tagged by the OP. I am upvoting your answer – Ma0 Aug 10 '16 at 11:19
0

here you get a list of all the desired lists

list = ['Root Cause: Hardware Failure\nAction Completed: Power supply/filter/cable swap']
temp = L[0].split('\n')
newList = []
for n in temp:
    newList.append([n])

and you will get:

newList = [['Root Cause: Hardware Failure'], ['Action Completed: Power supply/filter/cable swap']]
Tom
  • 21
  • 4