为什么此代码会抛出无效的语法错误?我很确定大约三个月前它运行良好。关于字典,Python语言有什么变化吗?错误如下:
File "code.py", line 4
'den'{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\
SyntaxError: invalid syntax
如果我注释掉第4行和第5行,则错误指向第7行。错误始终引用第二个单引号字符。在第四行中,它指向" '
"中的N之后的den
。
以下是代码:
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,\
'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9}\
\
'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\
'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},\
\
'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,\
'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},\
\
'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,\
'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},\
\
'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,\
'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},\
\
'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,\
'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},\
\
'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,\
'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},\
\
'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,\
'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},\
\
'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,\
'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},\
\
'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,\
'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},\
\
'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,\
'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},\
\
'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,\
'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},\
\
'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,\
'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1}
\
'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,\
'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9}
\
'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,\
'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}
答案 0 :(得分:3)
在... 'cen':.9} ,\
\
den:{...
之后的近距离大括号之后,您似乎错过了一个逗号。试试这个修复:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script type="text/javascript">
window.onload = function () {
var img = document.getElementById('embedImage');
var button = document.getElementById('saveImage');
img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA'+
'AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO'+
'9TXL0Y4OHwAAAABJRU5ErkJggg==';
img.onload = function () {
button.removeAttribute('disabled');
};
button.onclick = function () {
window.location.href = img.src.replace('image/png', 'image/octet-stream');
};
};
</script>
</head>
<body>
<img id="embedImage" alt="Red dot"/>
<input id="saveImage" type="button" value="save image" disabled="disabled"/>
</body>
</html>
我很确定这个文件在这种情况下三个月前无法正常运行。
答案 1 :(得分:2)
您在字符串'den'
之前丢失了'cen':.9
。
答案 2 :(得分:2)
通过明智地使用空格,可以更容易地发现这种语法错误。至少,错误信息可以让您更好地了解在哪里寻找。
milevalue = {
'adm': {
'adm': 0,
'den': 2.4,
'ear': 3,
'edi': 3.2,
'hug': 2.6,
'fra': 2.1,
'the': 1.2,
'hor': 3.4,
'lon': 1.5,
'rid': 7.7,
'hig': 1.8,
'tho': 5.5,
'was': 1.8,
'act': 0.8,
'cen': .9
} # Oops, here's the missing comma
'den': {
'adm': 2.4,
'den': 0,
'ear': 3.5,
'edi': 3.6,
'hug': 1.6,
'fra': 3.0,
# etc
答案 3 :(得分:1)
你错过了一些逗号,修复:
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9},'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1},'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9},'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}
答案 4 :(得分:1)
代码已修复。
缺少逗号,“,”请小心使用它们,或者您应该使用字典表示法生成字典。
milevalue = {'adm':{'adm':0,'den':2.4,'ear':3,'edi':3.2,'eug':2.6,'fra':2.1,'thu':1.2,\ 'hor':3.4,'lon':1.5,'rid':7.7,'hig':1.8,'tho':5.5,'was':1.8,'aca':0.8,'cen':.9}\ \ 'den':{'adm':2.4,'den':0,'ear':3.5,'edi':3.6,'eug':1.6,'fra':3.0,'thu':3.1,\ 'hor':1.6,'lon':1.4,'rid':7.4,'hig':2.3,'tho':5.8,'was':0.7,'aca':2.6,'cen':1.8},\ \ 'ear':{'adm':3,'den':3.5,'ear':0,'edi':0.1,'eug':1.9,'fra':0.9,'thu':2.1,\ 'hor':2.9,'lon':4.4,'rid':4.8,'hig':1.4,'tho':2.7,'was':3.2,'aca':2.4,'cen':2.5},\ \ 'edi':{'adm':3.2,'den':2.4,'ear':0.1,'edi':0,'eug':2.0,'fra':1.1,'thu':2.3,\ 'hor':2.7,'lon':4.5,'rid':4.7,'hig':1.5,'tho':2.5,'was':3.4,'aca':2.5,'cen':2.6},\ \ 'eug':{'adm':2.6,'den':1.6,'ear':1.9,'edi':2.0,'eug':0,'fra':1.8,'thu':3.0,\ 'hor':1,'lon':2.6,'rid':6.4,'hig':1.4,'tho':4.3,'was':1.4,'aca':2.5,'cen':1.7},\ \ 'fra':{'adm':2.1,'den':3.0,'ear':0.9,'edi':1.1,'eug':1.8,'fra':0,'thu':1.2,\ 'hor':2.8,'lon':3.4,'rid':5.7,'hig':0.4,'tho':3.6,'was':2.3,'aca':1.5,'cen':1.6},\ \ 'thu':{'adm':1.2,'den':3.1,'ear':2.1,'edi':2.3,'eug':3.0,'fra':1.2,'thu':0,\ 'hor':4,'lon':2.9,'rid':7.1,'hig':1.6,'tho':5.1,'was':2.4,'aca':0.6,'cen':1.6},\ \ 'hor':{'adm':3.4,'den':1.6,'ear':2.9,'edi':2.7,'eug':1,'fra':2.8,'thu':4,\ 'hor':0,'lon':2.7,'rid':6.1,'hig':2.4,'tho':4.4,'was':2,'aca':3.5,'cen':2.8},\ \ 'lon':{'adm':1.5,'den':1.4,'ear':4.4,'edi':4.5,'eug':2.6,'fra':3.4,'thu':2.9,\ 'hor':2.7,'lon':0,'rid':8.6,'hig':3.5,'tho':7.1,'was':1.6,'aca':2.5,'cen':2.5},\ \ 'rid':{'adm':7.7,'den':7.4,'ear':4.8,'edi':4.7,'eug':6.4,'fra':5.7,'thu':7.1,\ 'hor':6.1,'lon':8.6,'rid':0,'hig':5.9,'tho':2.4,'was':7.8,'aca':6.9,'cen':7},\ \ 'hig':{'adm':1.8,'den':2.3,'ear':1.4,'edi':1.5,'eug':1.4,'fra':0.4,'thu':1.6,\ 'hor':2.4,'lon':3.5,'rid':5.9,'hig':0,'tho':3.8,'was':1.9,'aca':1.3,'cen':1.1},\ \ 'tho':{'adm':5.5,'den':5.8,'ear':2.7,'edi':2.5,'eug':4.3,'fra':3.6,'thu':5.1,\ 'hor':4.4,'lon':7.1,'rid':2.4,'hig':3.8,'tho':0,'was':5.9,'aca':4.8,'cen':4.9},\ \ 'was':{'adm':1.8,'den':0.7,'ear':3.2,'edi':3.4,'eug':1.4,'fra':2.3,'thu':2.4,\ 'hor':2,'lon':1.6,'rid':7.8,'hig':1.9,'tho':5.9,'was':0,'aca':1.9,'cen':1.1}, \ 'aca':{'adm':0.8,'den':2.6,'ear':2.4,'edi':2.5,'eug':2.5,'fra':1.5,'thu':0.6,\ 'hor':3.5,'lon':2.5,'rid':6.9,'hig':1.3,'tho':4.8,'was':1.9,'aca':0,'cen':0.9}, \ 'cen':{'adm':0.9,'den':1.8,'ear':2.5,'edi':2.6,'eug':1.7,'fra':1.6,'thu':1.6,\ 'hor':2.8,'lon':2.5,'rid':7,'hig':1.1,'tho':4.9,'was':1.1,'aca':0.9,'cen':0}}
答案 5 :(得分:0)
在我的例子中,一个大字典的值之一包含一个未转义的单引号,如 'today's'
。通过转义 'today\'s'
来修复它(双引号也有效)。以防万一它对某人有帮助,这些小问题很容易在大字典中遗漏,特别是如果使用其他代码自动生成。