AttributeError:’bytes’ object has no attribute ‘encode’

If you don’t know if a stringlike object is a Python 2 string (bytes) or Python 3 string (unicode). You could have a generic converter. Python3 shell: On Python 2 both these expressions evaluate to True: type(“hello”) == bytes and type(“hello”) == str. And type(u”hello”) == str evaluates to False, while type(u”hello”) == unicode is True. On Python 3 type(“hello”) == bytes is False, and type(“hello”) == str is True. And type(“hello”) … Read more

How to create an empty R vector to add new items

I want to use R in Python, as provided by the module Rpy2. I notice that R has very convenient [] operations by which you can extract the specific columns or lines. How could I achieve such a function by Python scripts? My idea is to create an R vector and add those wanted elements into this … Read more

How to update a plot in matplotlib?

You essentially have two options: Do exactly what you’re currently doing, but call graph1.clear() and graph2.clear() before replotting the data. This is the slowest, but most simplest and most robust option. Instead of replotting, you can just update the data of the plot objects. You’ll need to make some changes in your code, but this should be much, much … Read more

How to “test” NoneType in python?

So how can I question a variable that is a NoneType? Use is operator, like this Why this works? Since None is the sole singleton object of NoneType in Python, we can use is operator to check if a variable has None in it or not. Quoting from is docs, The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is … Read more