0

I found what I hoped would be the answer here.

In my case I have 2 lists of strings. From this code:

fields = POST[ 'fields' ]
values = POST[ 'row' ]

for ( field, value ) in zip( fields, values ):
    f.write(   f'  field {field}, value {value}\n' )

... this is what I get:

row: [4,1,"AAR",null,null,"Aardvark Ltd",null,"2019-07-20 00:00:00","modem - ring first","M",null,null,"",null,null]
fields: ["id","category","code","title","firstname","secondname","salutation","lastcon","sendconventions","stdsendmethod","vatno","refquote","notes","created","lastmodif"]
  field [, value [
  field ", value 4
  field i, value ,
  field d, value 1
  field ", value ,
  field ,, value "
  field ", value A
  field c, value A
  field a, value R
  field t, value "
  ...

It's picking apart the strings into characters, and even including the square brackts, quotes and commas. How might I prevent this? I want it to list the pairs of strings (1 from each list) as it iterates.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • If it's doing that, `fields` and `row` aren't lists, they're strings. To convert them, you need [here](https://stackoverflow.com/questions/1894269/how-to-convert-string-representation-of-list-to-a-list). – Carcigenicate Jun 04 '20 at 13:34

3 Answers3

0

I'm assuming that you're getting those fields and values from a http request body. what you can do in that case is just use literal_eval https://docs.python.org/3/library/ast.html#ast.literal_eval to convert the data to actual python lists and iterate over them.

In your case:

from ast import literal_eval
fields = literal_eval(POST['fields'])
values = literal_eval(POST['row'])

and then try to iterate over them.

Underoos
  • 4,708
  • 8
  • 42
  • 85
  • Thanks... yes, indeed. I had assumed `json.loads` would magic up the objects in pristine fashion. I'm sure yours is the way to go but I'm getting an error currently: "'ValueError'> thrown: malformed node or string: <_ast.Name object at 0x7f1935fcf048>" ... investigating... – mike rodent Jun 04 '20 at 13:46
  • I'm assuming that's because `null` in the string instead of `None`. – Underoos Jun 04 '20 at 13:48
  • Right... and converting occurrences of "null" to "None" would be distinctly non-trivial: inside one of the strings, or a genuine null element of the list? Wonder whether something in `json` handles this situation? – mike rodent Jun 04 '20 at 13:58
  • yes. https://docs.python.org/3/library/json.html#json.loads this should do the work. – Underoos Jun 04 '20 at 14:28
  • 1
    I had made the mistake of double-stringifying in JS. All works fine now, without any need for `literal_eval`. Apologies for wasting your time. – mike rodent Jun 04 '20 at 14:30
0

@mike: Did you check to confirm that they are indeed lists in the first place? You can do that by using the following command:

    print(type(fields)) 
    print(type(values)) 

The general approach of using zip to loop over two lists and the way you are doing it is indeed correct. However, I think you are handling an object from requests package, but not lists.

UGuntupalli
  • 769
  • 1
  • 9
  • 22
0

Based on your output, fields and values are strs and not lists. A solution might involved turning the values into lists using python's ast.literal_eval.

from ast import literal_eval
fields = literal_eval(POST['fields'])
values = literal_eval(POST['row'])

for field, value in zip(fields, values):
    f.write(f'  field {field}, value {value}\n')

More info about ast.literal_eval can be found here.

Fady Adal
  • 345
  • 1
  • 10