1

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! If you want more information, please tell me.

python code:

#! /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
martineau
  • 119,623
  • 25
  • 170
  • 301
tktktk0711
  • 1,656
  • 7
  • 32
  • 59
  • Possible duplicate of [Python star unpacking for version 2.7](http://stackoverflow.com/questions/10792970/python-star-unpacking-for-version-2-7) – Matthias Apr 19 '17 at 11:55

2 Answers2

8

Extended Iterable Upacking is only available in Python 3.0 and later.

Refer to this question for workarounds.

Community
  • 1
  • 1
timgeb
  • 76,762
  • 20
  • 123
  • 145
2

The script is using something called "Extended Iterable Unpacking" which was added to Python 3.0.
The feature is described in PEP 3132.

To do the same thing in Python 2, replace the problem line:

    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:]
martineau
  • 119,623
  • 25
  • 170
  • 301