0

I have a function which takes 2 parameters; a dict- _tInput, and a list- _order.

The aim is to be able to order the returned data for printing:

tOrderReq = [1, 4, 5, 2]
tReturnData = tOut(hInput[0], (tOrderReq))
print("%s %s %s %s" % tReturnData) 

However I am getting the following error:

Traceback (most recent call last):
File "C:\local\mdev.py", line 76, in <module>
TypeError: not enough arguments for format string

I don't understand what I need to change to get this to work, I suspect it is something fairly basic that is showing through with my novice Python knowledge and my understanding of tuples, lists and dicts. The function is below-- incidentally, if there are any tips on how to improve this code, then i'm all ears.

def tOut(_tInput, _order=[]):
    _tOutput = []
    _tOutput.append(_tInput['id'])
    _tOutput.append(_tInput['created_at'].encode('utf-8'))
    _tOutput.append(_tInput['text'].encode('utf-8'))
    _tOutput.append(_tInput['user']['id'])    
    _tOutput.append(_tInput['user']['screen_name'].encode('utf-8'))
    _tOutput.append(_tInput['user']['name'].encode('utf-8')) 

    _tReturn = []
        for x in _order:
        _tReturn.append(_tOutput[x])

    return (_tReturn)

Many thanks

Oli
  • 13
  • 2
  • 2
    Did you perchance call it with `tOut(hInput[0], [tOrderReq])` instead? What is the *full* traceback? – Martijn Pieters May 08 '14 at 16:04
  • If it is [tOrderReq] instead, the error is : TypeError: not enough arguments for format string. The traceback is as I pasted above – Oli May 08 '14 at 16:13
  • 1
    I didn't say to use that instead, I was trying to hazard a guess as to why you see your exception, because you didn't provide us with enough information. What is the full traceback? – Martijn Pieters May 08 '14 at 16:14
  • 1
    You didn't provide the **full** traceback, only the last two lines. A full traceback starts with the line *Traceback (most recent call last):*. – Martijn Pieters May 08 '14 at 16:15
  • Unrelated, but: as a rule, don't use mutable default arguments like `_order=[]`. They'll get you into trouble sooner or later (see [here](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-which-scope-is-the-mutable-default-argument-in) for the details.) – DSM May 08 '14 at 16:18
  • Martijn, I've updated the main post-- it seems I had a cut&paste error, so I'd given a different error, the correct one is now there. Thanks for looking! – Oli May 08 '14 at 16:22
  • I apologise, my mistake was most likely pointing you in the wrong direction, the answer below was simply to have tuple(tReturnData). But I do appreciate the help you offered! – Oli May 08 '14 at 16:24

1 Answers1

0

IIUC, your problem is that Python needs a tuple with %-interpolation:

>>> tReturnData = [1,2,3,4]
>>> print("%s %s %s %s" % tReturnData)
Traceback (most recent call last):
  File "<ipython-input-13-a0f54ded128d>", line 1, in <module>
    print("%s %s %s %s" % tReturnData)
TypeError: not enough arguments for format string

>>> print("%s %s %s %s" % tuple(tReturnData))
1 2 3 4

If (as it seems you are) you're using Python 3, you could simply write

>>> print(*tReturnData)
1 2 3 4

or use str.format (which is the preferred modern style for finer control, which admittedly we're not using here):

>>> print("{} {} {} {}".format(*tReturnData))
1 2 3 4
DSM
  • 342,061
  • 65
  • 592
  • 494
  • Thank you, it was tuple(tReturnData) that was the fix-- incidentally, I am using 2.7.6, is there something that i've written that is more of a Python3 standard? – Oli May 08 '14 at 16:25
  • @Oli: the use of parentheses with `print` is more common in 3 as `print` is now a function and so needs them. – DSM May 08 '14 at 16:28