简体   繁体   中英

How to delete rows in CSV-Output in Python

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())
´´´

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM