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

Time:10-07

The error code
Def analyseLocation (friends) :
Freqs={}
Headers=[' NickName ', 'Province', 'City']
With the open (' location. CSV ', 'w', encoding="utf-8", newline=',) as csvFile:
Writer.=the CSV DictWriter (csvFile, headers)
Writer. Writeheader ()
For friend friends in [1] :
The row={}
Row [' NickName ']=friend [' NickName ']
Row [' Province ']=friend [' Province ']
Row [' City ']=friend [' City ']
If (friend [' Province ']!=None) :
If (friend [' Province '] not in freqs) :
Freqs [friend [' Province ']]=1
The else:
Freqs [friend [' Province ']]=1
Writer. Writerow (row)
For k, v in freqs:
Print (" {0}, {1} ". The format (k, v))

An error prompt
Traceback (the most recent call last) :
The File "main. Py", line 195, in & lt; module>
AnalyseLocation (friends)
The File "main. Py", line 64, in analyseLocation
For k, v in freqs:
ValueError: not enough values to unpack (expected 2, got 0)

Complete code for the following
#! The/usr/bin/python
# - * - coding: utf-8 - * -

The import OS
The import re
The import CSV
The import time
The import json
The import jieba
The from jieba import analyse
The import itchat
The import base64
The from snownlp import snownlp
The import requests
The import sys
The from the collections import Counter
The import matplotlib. Pyplot as PLT
The from pylab import *
The from faceApi import faceApi
MPL. RcParams [' font. Sans-serif]=[' SimHei]
The from PIL import Image
The import numpy as np
The from wordcloud import wordcloud

Def analyseSex (firends) :
Sexs=list (map (lambda x: x [' Sex '], friends [1]))
Counts=Counter (sexs). The items ()
Counts=sorted (counts, key=lambda x: x [0], reverse=False)
Counts=list (map (lambda x: x [1], counts))
Labels=[' Unknow 'and' Male 'and' getting ']
Colors=[' red ', 'yellowgreen', 'lightskyblue]
PLT. Figure (figsize=(8, 5), dpi=80)
PLT. Axes (aspect=1)
PLT. Pie (counts,
Labels=labels,
Colors=colors,

labeldistance=1.1,Autopct='% % % 3.1 f,
Shadow=False,
Startangle=90,
Pctdistance=0.6
)
PLT. Legend (loc='upper right')
PLT. Title (u '% s WeChat friends sex of' % friends [0] [' NickName '])
PLT. The show ()

Def analyseLocation (friends) :
Freqs={}
Headers=[' NickName ', 'Province', 'City']
With the open (' location. CSV ', 'w', encoding="utf-8", newline=',) as csvFile:
Writer.=the CSV DictWriter (csvFile, headers)
Writer. Writeheader ()
For friend friends in [1] :
The row={}
Row [' NickName ']=friend [' NickName ']
Row [' Province ']=friend [' Province ']
Row [' City ']=friend [' City ']
If (friend [' Province ']!=None) :
If (friend [' Province '] not in freqs) :
Freqs [friend [' Province ']]=1
The else:
Freqs [friend [' Province ']]=1
Writer. Writerow (row)
For k, v in freqs:
Print (" {0}, {1} ". The format (k, v))

Def analyseHeadImage (frineds) :
# Init Path
BasePath=OS. Path. Abspath ('. ')
BaseFolder=basePath + '\ \ HeadImages \ \'
If (OS. Path. The exists (baseFolder)==False) :
OS. Makedirs (baseFolder)

# Analyse Images'
FaceApi=faceApi ()
Use_face=0
Not_use_face=0
Image_tags='
For the index in range (1, len (friends)) :
Friend=friends [index]
# Save HeadImages
ImgFile=baseFolder + '\ \ Image % s.j pg % STR (index)
ImgData=https://bbs.csdn.net/topics/itchat.get_head_img (userName=friend [' userName '])
If (OS. Path. The exists (imgFile)==False) :
With the open (imgFile, 'wb) as file:
File. The write (imgData)

# Detect Faces
Time. Sleep (1)
Result=faceApi. DetectFace (imgFile)
If the result==True:
Use_face +=1
The else:
Not_use_face +=1

# Extract Tags
Result=faceApi. ExtractTags (imgFile)
Image_tags +=', '. Join (list (map (lambda x: x [' tag_name], the result)))

Labels=[u 'use of human face image, u' do not use the human face image]
Counts=[use_face not_use_face]
Colors=[' red ', 'yellowgreen', 'lightskyblue]
PLT. Figure (figsize=(8, 5), dpi=80)
PLT. Axes (aspect=1)
PLT. Pie (counts, # gender statistics
Gender show labels=labels, # tag
Colors=colors, color # pie chart area
Labeldistance=1.1, # label distance dot distance
Autopct='% % % 3.1 f, text format # pie chart area
Shadow=False, whether # pie chart shows the shadow
Startangle=90, # pie chart starting Angle
Pctdistance=0.6 # text from the pie chart area dot distance
)
PLT. Legend (loc='upper right')
PLT. Title (u '% s WeChat friends use facial image' % friends [0] [' NickName '])
PLT. The show ()

Image_tags=image_tags. Encode (' iso8859-1). Decode (' utf-8)
Back_coloring=np. Array (Image. Open (' face. JPG))
Wordcloud=wordcloud (
Font_path='simfang. The vera.ttf',
Background_color="white",
Max_words=1200,
Mask=back_coloring,
Max_font_size=85,
Random_state=75,
Width=800,
Height=480,
Margin=15
)

Wordcloud. Generate (image_tags)
PLT. Imshow (wordcloud)
PLT. Axis (" off ")
PLT. The show ()

Def analyseSignature (friends) :
Signatures='
Emotions=[]
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related