Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading