Home > Back-end >  How to delete rows in CSV-Output in Python
How to delete rows in CSV-Output in Python

Time:11-04

I have the following problem: When I run my program 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('Trump')

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

with open('Outputfile.csv', 'w', newline='') as file:
    alpha = csv.DictWriter(file, keys)
    alpha.writeheader()
    alpha.writerows(googlenews.results())
´´´

CodePudding user response:

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)
  • Related