How to delete rows in CSV-Output in Python

I have the following problem: When I run my program it works well, but in the output CSV are some columns which i dont want to have in my output csv.

googlenews = GoogleNews()
googlenews.set_encode('utf_8')
googlenews.get_news('Air Wings')

keys = googlenews.results()[0].keys()

with open('Output.csv', 'w', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, keys)
    dict_writer.writeheader()
    dict_writer.writerows(googlenews.results())
´´´

>Solution :

You’re thinking too hard. All I mean is something like this:

newlist = []
for row in googlenews.results():
    newlist.append( { 'title': row['title'], 'date': row['date' } )

with open('Output.csv', 'w', newline='') as output_file:
    dict_writer = csv.DictWriter(output_file, newlist[0].keys())
    dict_writer.writeheader()
    dict_writer.writerows(newlist)

Leave a Reply