-1

I need to create a list of lists which can split a large string by newline first and then semi colon. I have a list of strings by splitting input by newline. I need to now take those elements in that list and split them by semi colon but is not letting me split again.

AttributeError: 'list' object has no attribute 'split'

items = sys.stdin.read()

collectionList = [(items.split('\n'))]

for item in collectionList:
    item.split(':')

2 Answers2

1

Try changing the second line to

collectionList = items.split( '\n' )

The split method automatically returns a list, so you don't need to encolse items.split( '\n' ) in square brackets. Also, you might want to store the result of each semicolon splitting in another list or some other kind of variable, for further processing:

results = []
for item in collectionList:
    results.append( item.split( ':' ) )
  • Thanks! I had it enclosed after reading another post forgot to remove the brackets :\. The second part of your answer is what I was looking for, but is there any way of doing it without creating another list? I would like to perform that on the same list if possible. – user3503046 May 11 '15 at 03:31
  • You're most welcome. Well, you need the original list in order to iterate through its elements. You could add the following line to the code: `collectionList = results`. – Fernando Karpinski May 11 '15 at 03:48
0

Change the second line for this line

collectionList = items.split('\n')
cosmoscalibur
  • 1,131
  • 1
  • 8
  • 17