Search code examples
pythonprintingapostrophe

python trying to remove single apostrophe


I'm using a program called CityEngine which has a python element to it.

The problem: I've just called a function on an object and returns me a list of numbers, xyz. I split the xyz into their own names. I also call a function to retrieve a different attribute related to this object to replace the previously retrieved y value.

Now, when I print the y value, it contains numerical characters only apart from decimal place. When I incorporate the y value into a new list, it's value has single apostrophe around it.

For example, print(y) returns 5.0000000

If I place it like this position[x,y,z] I get a print(position) of [0, '5.000000' , 0]. The program can't read the single apostrophes so ignored the value completely.

I've tried .remove("'","") and .strip() and nothing.

Any help would be appreciated.

Thanks.


Solution

  • That looks more as if the function were not returning a number but a string. So, in order to deal with it, you’ll have to convert the string using either int() or float().

    In general, if you do a print(l) on some list of items, each item will be printed with the output of it’s __repr__ method. Convention has it that the __repr__ method of string wraps the string with single apostrophes, whereas numbers do not get wrapped. This is to remove potential ambiguity. Hence, a print(l) which returned

    [0, '5.00000', 0.1]
    

    would be a list containing an int, a str and a float.