python 中的语法错误:',' 后缺少空格

Syntax Error in python: missing whitespace after ','

谁能帮我改正语法错误。我在这一行收到语法错误。我不确定为什么会出现语法错误,但是当我检查在线工具是否存在 python 错误时,它还指出我有语法错误并且在 ','

之后缺少空格

下面是代码片段:

d = []  
matches = matcher(doc)
for match_id, start, end in matches:
    rule_id = nlp.vocab.strings[match_id]  # get the unicode ID, i.e. 'COLOR'
    span = doc[start : end]  # get the matched slice of the doc
    d.append((rule_id, span.text))      
keywords = "\n".join(f'{i[0]} {i[1]} ({j})' for i,j in Counter(d).items())

我在这一行收到语法错误:

keywords = "\n".join(f'{i[0]} {i[1]} ({j})' for i,j in Counter(d).items())

语法错误:语法无效

您正在使用的 f-string syntax 是在 Python 3.6 中引入的。您要么必须升级 Python 版本,要么使用不同的字符串格式化技术。

另一种方法是 str.format() 方法:

keywords = "\n".join('{} {} ({})'.format(i[0], i[1], j) for i,j in Counter(d).items())

还有old printf-style formatting方法:

keywords = "\n".join('%s %s (%s)' % (i[0], i[1], j) for i,j in Counter(d).items())