python - python中的语法错误:','后缺少空格
问题描述
有人可以帮我纠正语法错误吗?我在这一行收到语法错误。我不知道为什么会出现语法错误,但是当我检查在线工具是否有 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())
SyntaxError:无效的语法
解决方案
您使用的f-string 语法是在 Python 3.6 中引入的。您要么必须升级 Python 版本,要么使用不同的字符串格式化技术。
一种替代str.format()
方法是:
keywords = "\n".join('{} {} ({})'.format(i[0], i[1], j) for i,j in Counter(d).items())
还有旧的 printf 样式格式化方法:
keywords = "\n".join('%s %s (%s)' % (i[0], i[1], j) for i,j in Counter(d).items())
推荐阅读
- javascript - 在输入字段中使用 dd/mm/yyyy 输入日期格式
- java - 无法与 OnClick\OnItemClickListener 中的视图及其事件交互
- neo4j - 从记录列表中创建节点时,如何传递 datetime 属性?
- java - 如何将类的对象分配给值
- flask - 用户的烧瓶个人资料页面
- java - 更改片段 - 如果可能,请改进它
- matlab - x * x ^ T的Matlab符号计算
- c# - 如果字符是大写,则拆分 camelCase str 正则表达式?
- c# - 使用排序算法在 Unity 中说明球的排序过程
- python - tensorflow conv2d的padding策略是什么?