0

Hello I am currently scraping information from online and putting it into a list however the elements are appearing as :

['\n      Michael\n    ', 
'\n      Elad\n    ', 
'\n      Yair\n    ',  
'\n      Liron\n    ',  
'\n      Idan\n    ',  
'\n      Tomer\n    ',  
'\n      Ofer\n    ',  
'\n      Asaf\n    ',  
'\n      Tomer\n    ',  
'\n      Ronit\n    ',  
'\n      Guy\n    ',  
'\n      Lior\n    ']

Is there a way to format the list to erase the line indentations and the white space?

Thank you

Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
  • 1
    `[x.strip() for x in list]` – Alan Kavanagh Jul 11 '18 at 12:05
  • Hello Ajejandro, used `strip()` for the `\n`, `lstrip()` for leading space and `rstrip()` for trailing spaces; `listing = ['\n Michael\n ', '\n Elad\n ', '\n Yair\n ', '\n Liron\n ', '\n Idan\n ', '\n Tomer\n ', '\n Ofer\n ', '\n Asaf\n ', '\n Tomer\n ', '\n Ronit\n ', '\n Guy\n ', '\n Lior\n ']` `new_list = [((item.rstrip()).lstrip()).strip('\n') for item in listing]` `print(new_list)` – Zack Atama Jul 11 '18 at 12:33

1 Answers1

0

Use the .strip() method:

>>> l = ['\n      Michael\n    ', '\n      Elad\n    ']
>>> l
['\n      Michael\n    ', '\n      Elad\n    ']
>>> [x.strip() for x in l]
['Michael', 'Elad']
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65