我试图在python2.7中读取一个文件,并且它完美无缺.我遇到的问题是当我在Python3.4中执行相同的程序然后出现错误:
'utf-8' codec can't decode byte 0xf2 in position 424: invalid continuation byte'
Run Code Online (Sandbox Code Playgroud)
此外,当我在Windows中运行该程序(使用python3.4)时,不会出现错误.该文件的第一行是:
Codi;Codi_lloc_anonim;Nom
我的程序代码是:
def lectdict(filename,colkey,colvalue):
f = open(filename,'r')
D = dict()
for line in f:
if line == '\n': continue
D[line.split(';')[colkey]] = D.get(line.split(';')[colkey],[]) + [line.split(';')[colvalue]]
f.close
return D
Traduccio = lectdict('Noms_departaments_centres.txt',1,2)
Run Code Online (Sandbox Code Playgroud) 我需要对函数进行内存使用分析。我正在使用带有Python 3.8.10的jupyter笔记本,并且我已经成功安装了memory_profiler 0.60,没有错误。当我加载 memory_profiler 时,使用%load_ext memory_profiler,没有出现错误,但是当我尝试使用 mprun ( %mprun -f suma2 suma2(0.2,0.2)) 时,出现此错误:
ERROR: Could not find file /tmp/ipykernel_75919/1494889556.py
Run Code Online (Sandbox Code Playgroud)
我想知道如何绘制网络(如果可以的话),但我想要一个饼图,而不是将圆圈作为节点,因为我有一个包含社区的图表,我想用工作人数来表示每个社区在每个部门。
编辑:好的,我刚刚试过这段代码,结果是好的:
import community
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
import random as rnd
import operator as op
import matplotlib.patches as mpatches
import math
Com = nx.Graph()
Com.add_nodes_from(['0','1','2','3','4'])
comlist=[('0','2',5.0),('3','0',3.0),('1','3',1.0),('2','3',7.3)]
Com.add_weighted_edges_from(comlist)
ListDeps = ['literature','maths','science','physical education']
HistCom = {'0':{'literature':20,
'maths':24,
'science':12},
'1':{'literature':1,
'physical education':14,
'science':6},
'2':{'science':15},
'3':{'physical education':4,
'maths':20},
'4':{'literature':20,
'maths':24,
'science':12}}
pos=nx.spring_layout(Com)
fig=plt.figure(figsize=(5,5))
ax=plt.axes([0,0,1,1])
ax.set_aspect('equal')
nx.draw_networkx_edges(Com,pos,ax=ax)
plt.xlim(-0.5,1.5)
plt.ylim(-0.5,1.5)
trans=ax.transData.transform
trans2=fig.transFigure.inverted().transform
piesize=0.1 #Degree
p2=piesize/2.0
dep_color = dict()
for com in Com:
xx,yy=trans(pos[com]) # figure coordinates
xa,ya=trans2((xx,yy)) …Run Code Online (Sandbox Code Playgroud)