简体   繁体   中英

Python dictionary throws invalid syntax

Why does this code throw an invalid syntax error? I am pretty sure this ran fine about three months ago. Has something changed in the Python language in regards to dictionaries? The error is as follows:

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

If I comment out lines 4 and 5 the error points to line 7. The error always references the second single quote character. In line four it points to the ' after the N in " den ".

Here is the code:

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}}

You seem to be missing a comma after the close brace after 'cen':.9 . Try this fix:

... 'cen':.9} ,\
\
den:{...

I'm pretty sure that this file, in this condition, would not have run correctly three months ago.

你输了,在字符串'den'之前。

Such syntax errors are much easier to spot with judicious use of whitespace. At the very least, the error message will you give you a better idea of where to look.

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

您错过了几个逗号,已修复:

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}}

Code Fixed.

Missing commas, "," please be careful with them or you should use dictionary notation to generate dictionaries.

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}}

In my case, one of the values of a large dict contained an unescaped single quote like 'today's' . Fixed it by escaping it 'today\\'s' (double quote also works). Just in case it helps someone, these small issues can easily be missed in a large dict, especially if generated automatically using another code.

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