H
H
hardwellZero2015-05-23 19:55:39
Python
hardwellZero, 2015-05-23 19:55:39

How to clean list?

Good evening.
I parse the headlines from a small article, and at the output I get a list like this:

['\nSave rainforests or they will worsen climate change, warns ex-WWF chief ', 
' \n$10m a minute given to fossil fuel companies in government subsidies ', 
' \n\xc2\xa0\n\nFossil fuel giants reject calls to keep oil, gas and coal in ground ',
 "\xc2\xa0\nSaudi oil minister: 'Fossil fuels doomed, we're switching to solar' ", 
' \nCoal and solar power executives clash at Paris climate conference ',
 ' \nUK cities should generate their own energy, National Trust says ', 
' \nCalifornia investigates nine-mile oil slick ', ' \nOxford Uni to stay away from oil and coal investments ',
 " \n\xc2\xa0\n\nGap between rich and poor 'keeps growing' ", 
' \nOil shock could push UK into deeper deflation, warns Bank of England ',
 " \nMcDonald's boss 'proud' of wages as thousands demand a pay rise ",
 ' \n\xc2\xa0\n\nFrance to force big supermarkets to give away unsold food to charity ', 
' \nTesco to cut soft drinks sugar content ', ' \nSugar tax to pay for treating obesity ',
 " \n\xc2\xa0\n\nAntarctic Peninsula in 'dramatic' ice loss ",
 ' \nVideo: Great Barrier Reef under the spotlight for World Turtle Day ', 
" \nHow 'upcycling' became the hot new craft industry ", 
' \nA grassroots effort to save Africa\xe2\x80\x99s most endangered ape ', 
' \n\xc2\xa0\n\n\nGreenhouse Pioneer: Canon Christopher Hall, Pension Activist ']

Can I somehow remove all \n\xc2\xa0 and other garbage from there?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-05-23
@hardwellZero

The re module is there, for those who are advanced. For the rest, str.replace.

S
sim3x, 2015-05-23
@sim3x

there is no need to delete anything

l = ['\nSave rainforests or they will worsen climate change, warns ex-WWF chief ', 
   ...: ' \n$10m a minute given to fossil fuel companies in government subsidies ', ]

In [2]: ' || '.join(l)
Out[2]: '\nSave rainforests or they will worsen climate change, warns ex-WWF chief  ||  \n$10m a minute given to fossil fuel companies in government subsidies '

In [3]: print ' || '.join(l)

Save rainforests or they will worsen climate change, warns ex-WWF chief  ||  
$10m a minute given to fossil fuel companies in government subsidies 

In [4]:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question