简体   繁体   English

SyntaxError尝试使用python 2.7执行python 3代码

[英]SyntaxError trying to execute python 3 code with python 2.7

I run the python 3 code which from others code as following in the python 2.7 environment, there is error as following, please give me some hints how to solve it, thanks! 我在python 2.7环境中运行了python 3的其他代码,如下所示,有错误,请给我一些如何解决的提示,谢谢! If you want more information, please tell me. 如果您需要更多信息,请告诉我。

python code: python代码:

#! /usr/bin/env python

from __future__ import print_function
import argparse
from collections import defaultdict
import numpy as np
import os
import sys
import utils


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('target')
    args = parser.parse_args()

    target = defaultdict(list)
    for i, line in enumerate(sys.stdin):
        filename, score, *rect = line.strip().split()
        name, _ = os.path.splitext(filename)
        score = float(score)
        rect = tuple(map(float, rect))
        target[name].append((score, rect))

        if (i + 1) % 1000 == 0:
            print(i + 1, file=sys.stderr)

    for name in target.keys():
        target[name] = np.array(target[name], dtype=utils.dtype)
        target[name].sort(order=('score',))
        target[name][:] = target[name][::-1]

    np.savez_compressed(args.target, **target)

the error: 错误:

File "./scripts/lo.py", line 19
    filename, score, *rect = line.strip().split()
                     ^
SyntaxError: invalid syntax

Extended Iterable Upacking is only available in Python 3.0 and later. 扩展Iterable Upacking仅在Python 3.0及更高版本中可用。

Refer to this question for workarounds. 有关变通办法,请参阅问题。

The script is using something called "Extended Iterable Unpacking" which was added to Python 3.0. 该脚本使用的是添加到Python 3.0的“扩展的可迭代拆包”功能。
The feature is described in PEP 3132 . 该功能在PEP 3132中进行了描述。

To do the same thing in Python 2, replace the problem line: 要在Python 2中执行相同的操作,请替换问题行:

    filename, score, *rect = line.strip().split()

with these two lines: 这两行:

    seq = line.strip().split()
    filename, score, rect = seq[0], seq[1], seq[2:]

OR these two: 这两个:

   seq = line.strip().split()
   (filename, score), rect = seq[:2], seq[2:]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM