1

I have an array defined like this in Python.

keys = "setid","cntrct_id","version_nbr"

and the elements are pushed into array as expected.

print(keys)
('setid', 'cntrct_id', 'version_nbr')

But when am trying to insert quotes and split the elements with ',' seperated

am getting output like this

'"setid","cntrct_id","version_nbr"'

am expecting output like this:

"setid","cntrct_id","version_nbr"

I tried many ways,

(','.join('"' + x + '"' for x in keys))

','.join(map(lambda x: "\"" + x + "\"", keys))

','.join(['"%s"' % w for w in keys])

but everything is appending single quotes,

How should I avoid generating single quotes from output?

sacuL
  • 49,704
  • 8
  • 81
  • 106
Anand
  • 45
  • 4
  • 3
    `keys` is not an array, it's a tuple – sacuL Dec 10 '18 at 17:16
  • 2
    try printing your results, you'll see that the quotes are gone. They are the result of the _representation_ of the string in the python REPL – Jean-François Fabre Dec 10 '18 at 17:18
  • You appear to be confusing the data structure with its string representation. You can get the output you want with `','.join('"{}"'.format(x) for x in keys)`, if you really do want a simple comma-separate list of quoted words. – chepner Dec 10 '18 at 17:18
  • first eval is evil, then there is no problem at all. The quotes aren't there. The _representation_ of the string adds them (representation is here to debug the result, not to print it) @nixon – Jean-François Fabre Dec 10 '18 at 17:26
  • keys = "setid","cntrct_id","version_nbr" am looking for this result exactly, df_full_01.join(df_cdc_update01,["setid","cntrct_id","version_nbr"],"leftanti") but if I add quotes seperated by comma its returning like this: df_full_01.join(df_cdc_update01,['"setid","cntrct_id","version_nbr"'],"leftanti") – Anand Dec 10 '18 at 19:05

1 Answers1

1

I think the ' is just from the Python shell and not really part of the string itself. Have a look at the following example:

Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> test_string = "hello"
>>> test_string
'hello'
>>> print(test_string)
hello
>>> 
finefoot
  • 9,914
  • 7
  • 59
  • 102