简体   繁体   中英

Unwanted assignment of double quote in string

So I have a list like this:

['a', 'b', 'c', 'd', 'e' ]

I have converted this list into a string:

"'a', 'b', 'c', 'd', 'e'"

The double inverted comma around the string only exists to wrap around the single inverted comma- I need the single inverted commas. The issue is when I input this string to a package that only takes this:

'a', 'b', 'c', 'd', 'e'

Of course, there is an error. Now, I have tried .replace(' " ', ' ') , string slicing , and all others mentioned here . Nothing works as the double quotation is not part of the string but only exists to print it and is carried to the variable during the assignment.

EDIT:

Here is part of how I converted the list to string:

string=''
# ...
string = string + (" '%s '," %(items) )

EDIT to add more information because of the following comment:

I am playing with this tool . In the following example all I am saying is that I am auto generating some variable to put in the third line but whatever I do, it invariably puts an additional comma around the string:

from dd import autoref as _bdd
bdd = _bdd.BDD()
bdd.declare('x', 'y', 'z') # ----------------------<this line>
u = bdd.add_expr('(x /\ y) \/ ~ z')
bdd.collect_garbage()  # optional
bdd.dump('awesome.pdf')

The error I get is:

AssertionError: undefined variable "a", known variables are:
 {"'x', 'y', 'z'": 0}

You will be able to generate this error if you simply replace the third line with this:

bdd.declare("'x', 'y', 'z'")

TL/DR: Need to provide single string names - solution further down.


The cool thing about a minimal reproducible example is I can easily see what you are trying to do:

The method bdd.add_expr(...) wants names of variables as single strings - you try to provide a string with combined names which won't work, no matter how you add or interleave string delimiters.

The link to the original module code is also really helpfull. There is no declare() method in bdd.py - so I looked at its constructor class BDD(_abc.BDD): and hunted the method down in its baseclass here: _abc.py - delcare implementation which gives you:

 def declare(self, *variables): """Add names in `variables` to `self.vars`. ```python bdd.declare('x', 'y', 'z') ``` """ for var in variables: self.add_var(var)

you can read more about this kind of parameter passing fe here:


Solution - provide the names instead:

from dd import autoref as _bdd

your_list = ['x', 'y', 'z']

bdd = _bdd.BDD()
bdd.declare(*your_list )   # provide list elements as single strings to function
u = bdd.add_expr('(x /\ y) \/ ~ z')
bdd.collect_garbage()  # optional
bdd.dump('awesome.pdf')

If you really want to go from a string, you need no string delimiters at all, simply provide the names:

from_string = "'x', 'y', 'z'" 

# this undoes all the stringifying of your list
as_list = from_string.translate(str.maketrans("", "", "',")).split()

bdd = _bdd.BDD()
bdd.declare(*as_list)   # provide list elements as single strings to function
u = bdd.add_expr('(x /\ y) \/ ~ z')
bdd.collect_garbage()  # optional
bdd.dump('awesome.pdf')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM