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
1

2 Answers 2

8

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

Refer to this question for workarounds.

Sign up to request clarification or add additional context in comments.

2 Comments

hi @timgeb thanks for your comments, how to solve this problem in python2
@tktktk0711 See the second part of my answer.
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:]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.