Home > other >  Help ah, ValueError: not enough values to unpack (expected 2, got 0)
Help ah, ValueError: not enough values to unpack (expected 2, got 0)

Time:09-24

The from the collections import defaultdict
The import matplotlib. Pyplot as PLT
The import CSV, sys, datetime
CsvFile=open (' D: \ Jupyternotebook \ P00000001 - ALL CSV ', 'r', encoding="utf-8") # open the file and use the open function is assigned to csvFile
Reader=CSV. DictReader (csvFile) # will csvFile reads in the form of a dictionary, assigned to reader
Obamadonations=defaultdict (lambda: 0) # to the default dictionary initialized obamadonations
For the row in reader: # for loop traverse reader,
Name=row [' cand_nm]
Datestr=row [' contb_receipt_dt]
Amount=float (row [' contb_receipt_amt '])
Date=datetime. Datetime. Strptime (datestr, '% d % b - % y) data in the # datestr installation' % d % b - % y 'date format
If the 'Obama' in name: # if statements, judgement of Mr Obama's donation
Obamadonations [date] +=amount # add the data of Mr Obama's donation obamadonations [date] assigned to obamadonations [date]
Sorted_by_date=sorted (obamadonations. The items (), key=lambda item: the item [0]) # sorted function of obamadonations. The items () sorting,
# items, it is obamadonations returns to list
X1, y1=zip (* sorted_by_date) # zip function, use zip (*) to disassemble sorted_by_date1 list
PLT. The plot (x1, y1, label='Obama') is generated by the plot function according to the x1 and y1 # figure, and labeled 'Obama'
PLT. Legend (loc='upper center', ncol=4) # rules using legend function generated by illustrations position in the whole figure, and the number of columns
# PLT. Savefig ('/TMP/test. The PNG, format='PNG') # in the console display
plt.show()
  • Related