How to convert ‘binary string’ to normal string in Python3?
Decode it. >>> b’a string’.decode(‘ascii’) ‘a string’ To get bytes from string, encode it. >>> ‘a string’.encode(‘ascii’) b’a string’
Decode it. >>> b’a string’.decode(‘ascii’) ‘a string’ To get bytes from string, encode it. >>> ‘a string’.encode(‘ascii’) b’a string’
As @jonrsharpe noted in a comment, this can be done with typing.Callable: from typing import Callable def my_function(func: Callable): Note: Callable on its own is equivalent to Callable[…, Any]. Such a Callable takes any number and type of arguments (…) and returns a value of any type (Any). If this is too unconstrained, one may … Read more
The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil. Try to use safer ways of parsing your input if possible.)
The problem is with the string “C:\Users\Eric\Desktop\beeline.txt” Here, \U in “C:\Users… starts an eight-character Unicode escape, such as \U00014321. In your code, the escape is followed by the character ‘s’, which is invalid. You either need to duplicate all backslashes: “C:\\Users\\Eric\\Desktop\\beeline.txt” Or prefix the string with r (to produce a raw string): r”C:\Users\Eric\Desktop\beeline.txt”
Bare * is used to force the caller to use named arguments – so you cannot define a function with * as an argument when you have no following keyword arguments. See this answer or Python 3 documentation for more details.
If you want to obtain the contents of a web page into a variable, just read the response of urllib.request.urlopen: import urllib.request … url=”http://example.com/” response = urllib.request.urlopen(url) data = response.read() # a `bytes` object text = data.decode(‘utf-8’) # a `str`; this step can’t be used if data is binary The easiest way to download and … Read more
The best way of appending a string to a string variable is to use + or +=. This is because it’s readable and fast. They are also just as fast, which one you choose is a matter of taste, the latter one is the most common. Here are timings with the timeit module: a = … Read more
According to the documentation, instead of execfile(“./filename”) Use exec(open(“./filename”).read()) See: What’s New In Python 3.0
This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement: print “Hello, World!” The statement above does not work in Python 3. In Python 3 you need to add parentheses around the value to be printed: print(“Hello, World!”) … Read more
Ubuntu 12.10+ and Fedora 13+ have a package called python3-pip which will install pip-3.2 (or pip-3.3, pip-3.4 or pip3 for newer versions) without needing this jumping through hoops. I came across this and fixed this without needing the likes of wget or virtualenvs (assuming Ubuntu 12.04): Install package python3-setuptools: run sudo aptitude install python3-setuptools, this … Read more