python - 移植到旧版本;语法问题

标签 python

我在2.7.9写了一个项目,现在要移植到2.4.3。我有一个语法问题的单行代码,我不确定它的哪一部分不符合 2.4.3。

代码:

file = open(fileName, 'r')
header = [a for a in file.readline().split(',')]
data = [{x : y for x, y in zip(header, line.strip('\n').split(','))} for line in file] 

错误:=

data = [{x : y for x, y in zip(header, line.strip('\n').split(','))} for line in file]
                ^
SyntaxError: invalid syntax

`

最佳答案

Python 2.4 中没有字典推导式。它们是在 Python 2.7 中引入的。

你可以重写

{x : y for x, y in zip(header, line.strip('\n').split(','))}

作为

dict(zip(header, line.strip('\n').split(',')))

关于python - 移植到旧版本;语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265849/

相关文章:

python - 获取 Pandas 中 groupby 操作的大小

Python App Engine 通过 Google Cloud Storage 提供文件服务

python - 有没有办法通过两个函数传递可选参数?

python - 在 python 中的三个重叠集中查找增量

python - 从大量条目创建可迭代列表

python - 将唯一的无序列集添加到列表

python tkinter选项菜单帮助-对象没有属性 '_root'

python - 在 pandas 中是否有类似 GroupBy.get_group 的东西,但有一个可选的默认值?

python - child 有多个外键给 parent ?

python - 对数组进行排序并将索引追溯到其排序顺序