Formatting the data structure to a comma separated list of arguments

I need to convert a list (or dict) to a comma separated list to switch to another language.

Is there a better way to do this than:

result = '' args = ['a', 'b', 'c', 'd'] i = 0 for arg in args: if i != 0: result += arg else: result += arg + ', ' i += 1 result = 'function (' + result + ') 

Thanks Dan

+6
python list refactoring
source share
4 answers
 'function(%s)' % ', '.join(args) 

produces

 'function(a, b, c, d)' 
+11
source share

', '.join(args) will do the trick.

+12
source share
 result = 'function (%s)' % ', '.join(map(str,args)) 

I recommend a map (str, args) instead of just args, because some of your arguments could potentially not be strings and might raise a TypeError, for example with an int argument in your list:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence item 0: expected string, int found 

When you fall into dict objects, you probably want a comma to separate the dict values ​​(presumably because the values ​​are what you want to pass to this function). If you execute the join method on the dict object itself, you will get keys divided like this:

 >>> d = {'d':5, 'f':6.0, 'r':"BOB"} >>> ','.join(d) 'r,d,f' 

You want the following:

 >>> d = {'d':5, 'f':6.0, 'r':"BOB"} >>> result = 'function (%s)' % ', '.join(map(str, d.values())) >>> result 'function (BOB, 5, 6.0)' 

Pay attention to the new problem that you are facing. When you pass a string argument through a join function, it loses its quotation mark. Therefore, if you plan on passing strings, you have lost the quotation marks that would normally surround the string when passed to the function (strings are quoted in many general purpose languages). However, if you only pass numbers, this is not a problem for you.

There is probably a better way to solve the problem I just described, but there is one method here that might work for you.

 >>> l = list() >>> for val in d.values(): ... try: ... v = float(val) #half-decent way of checking if something is an int, float, boolean ... l.append(val) #if it was, then append the original type to the list ... except: ... #wasn't a number, assume it a string and surround with quotes ... l.append("\"" + val + "\"") ... >>> result = 'function (%s)' % ', '.join(map(str, l)) >>> result 'function ("BOB", 5, 6.0)' 

Now the line has caves surrounding itself. If you pass in more complex types than numeric primitives and strings, then you probably need a new question :)

One last note: I used d.values ​​() to show how to extract values ​​from a dictionary, but in fact this will return the values ​​from the dictionary in almost random order. Since your function most likely requires arguments in a specific order, you must manually create your list of values ​​instead of calling d.values ​​().

+1
source share

Why not use a standard that both languages ​​can handle, such as JSON, XML, or YAML? simplejson is convenient and included as json in python 2.6.

0
source share

All Articles