Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 跳出空场_Python_List - Fatal编程技术网

Python 跳出空场

Python 跳出空场,python,list,Python,List,我有一些混乱的IMBD数据 我正在运行一个脚本,它将明确的字段(id、year和rank)隔离开来,并用引号将剩余的name字段括起来 我想修改脚本,在缺少数据的地方将0放在“rank”字段中。如果没有此更改,pop函数将无法计算字段,从而影响脚本计算和压缩名称字段的方式 如何将0添加到当前没有任何内容的“排名”字段中 这是数据的快照 id,name,year,rank 0,#28 (2002),2002, 1,#7 Train: An Immigrant Journey, The (2000)

我有一些混乱的IMBD数据

我正在运行一个脚本,它将明确的字段(id、year和rank)隔离开来,并用引号将剩余的name字段括起来

我想修改脚本,在缺少数据的地方将0放在“rank”字段中。如果没有此更改,
pop
函数将无法计算字段,从而影响脚本计算和压缩名称字段的方式

如何将0添加到当前没有任何内容的“排名”字段中

这是数据的快照

id,name,year,rank
0,#28 (2002),2002,
1,#7 Train: An Immigrant Journey, The (2000),2000,
2,$ (1971),1971,6.4000000000000004
3,$1,000 Reward (1913),1913,
4,$1,000 Reward (1915),1915,
5,$1,000 Reward (1923),1923,
6,$1,000,000 Duck (1971),1971,5
7,$1,000,000 Reward, The (1920),1920,
8,$10,000 Under a Pillow (1921),1921,
9,$100,000 (1915),1915,
10,$100,000 Pyramid, The (2001),2001,
11,$1000 a Touchdown (1939),1939,6.7000000000000002
这是我的工作脚本

f = open("IMDBMovie.txt")
print(next(f)) # header
for line in f:
    fields = line.strip().split(",")

    # Get unambiguous fields.
    id = fields.pop(0)
    rank = fields.pop(-1)
    year = fields.pop(-1)

    # Surround name with quotes.
    name = '"{}"'.format(",".join(fields))
    print("{},{},{},{}".format(id, name, year, rank))
使用该语法:

print("{},{},{},{}".format(id, name, year, rank if rank else 0))
#                                          ^^^^^^^^^^^^^^^^^^^
这是Python版本的